javaandroidandroid-fragmentslistenerpreferencefragment

How to trigger an event when clicked in PreferenceFragmentCompat


I use androidx. In the settings fragment, I want to create the "Preference" buttons and click on them to trigger some individual events.

How can I implement a click listener on a specific Preference?

Thats some my Code:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

        addPreferencesFromResource(R.xml.preference);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(R.color.graylight));

        Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.action_settings);
        toolbar.setLogo(R.drawable.ic_settings_white_24dp);

        PreferenceManager preferenceManager = getPreferenceManager();
        PreferenceScreen preferenceScreen = getPreferenceScreen();

        return view;
    }

}

And XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        android:title="@string/setting_person"
        app:initialExpandedChildrenCount="0"
        app:key="profile_set">

        <Preference
            android:id="@+id/preference2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:key="button1"
            android:summary="@string/setting_person_data"
            android:title="@string/setting_person_reg"
            app:icon="@drawable/ic_assignment" />
        <Preference
            android:key="button"
            android:summary="@string/setting_avatar"
            android:title="@string/setting_avatar_chg"
            app:icon="@drawable/ic_wallpaper_black_24dp" />

    </PreferenceCategory>

</PreferenceScreen>

As a result, I want to click on the trigger an event in MainActivity. But this is another question, now at least I should get a listen to the event, for example by calling Toast with the key of the button pressed.


Solution

  • After much torment, the solution was found as follows: (for AndroidX)

    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import androidx.appcompat.widget.Toolbar;
    import androidx.preference.Preference;
    import androidx.preference.PreferenceFragmentCompat;
    
    public class SettingsFragment extends PreferenceFragmentCompat {
    
            @Override
            public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    
                addPreferencesFromResource(R.xml.preference);
    
                Preference preferenceMap = findPreference("button");
    
                preferenceMap.setOnPreferenceClickListener(
                        new Preference.OnPreferenceClickListener() {
                            @Override
                            public boolean onPreferenceClick(Preference arg0) {
                                getActivity().onBackPressed();
                                ((MainActivity) getActivity()).injectSetting("map");
                                return true;
                            }
                        });
    
            }
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
                View view = super.onCreateView(inflater, container, savedInstanceState);
                view.setBackgroundColor(getResources().getColor(R.color.graylight));
    
                Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
                toolbar.setTitle(R.string.action_settings);
                toolbar.setLogo(R.drawable.ic_settings_white_24dp);
    
                return view;
            }
    
        }
    

    and in XML androidx.preference is added to the element:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <androidx.preference.PreferenceCategory
            android:title="@string/setting_person"
            app:initialExpandedChildrenCount="1"
            app:key="profile_set">
    
            <androidx.preference.Preference
                android:id="@+id/preference1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:key="button"
                android:summary="@string/setting_person_data"
                android:title="@string/setting_person_reg"
                app:icon="@drawable/ic_assignment" />
            <androidx.preference.Preference
                android:id="@+id/preference2"
                android:key="button2"
                android:summary="@string/setting_avatar"
                android:title="@string/setting_avatar_chg"
                app:icon="@drawable/ic_wallpaper_black_24dp" />
        </androidx.preference.PreferenceCategory>
    
    </androidx.preference.PreferenceScreen>
    

    And do not forget at build.gradle dependencies:

    implementation 'androidx.preference:preference:1.1.0'
    

    Maybe someone will need ))