Preference Fragment setOnPreferenceClickListener Not Working

3.8k views Asked by At

I am currently attempting to implement a Preference Fragment into my app and I am having trouble setting up the setOnPreferenceClickListener. It seems as if the listener is not working at all and the code is never being implemented. I simply just have a Toast in the listener for now as a test. I have browsed numerous forums and have not found a good answer thus far. Code is below. Thanks in advance!

SettingFragment.java

public class SettingsFragment extends PreferenceFragment {

    private OnFragmentInteractionListener mListener;

    private Preference mFontPreference;
    private Preference mFontSizePreference;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.fragment_settings);

        mFontPreference = findPreference("font_preference");
        mFontSizePreference = findPreference("font_size_preference");

        mFontPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                String key = preference.getKey();
                Toast.makeText(getActivity(), key, Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }

fragment_settings.xml

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

    <PreferenceCategory android:title="Text Settings">

        <ListPreference
            android:enabled="true"
            android:key="font_preference"
            android:title="Font"
            android:summary="Select a Font"
            android:entries="@array/font_spinner_array"
            android:entryValues="@array/font_spinner_array"/>

        <ListPreference
            android:enabled="true"
            android:key="font_size_preference"
            android:title="Font Size"
            android:summary="Select a Font Size"
            android:entries="@array/font_size_array"
            android:entryValues="@array/font_size_value_array"/>

    </PreferenceCategory>

</PreferenceScreen>

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="font_spinner_array">
        <item>System Font</item>
        <item>Helvetica</item>
        <item>Helvetica-Neue</item>
        <item>Impact</item>
    </string-array>
    <string-array name="font_size_array">
        <item>Small</item>
        <item>Medium</item>
        <item>Large</item>
    </string-array>
    <string-array name="font_size_value_array">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>

EDIT: So the Toast wasnt working because it didnt have the .show(). That was fixed and it displays "font_preference" when clicked. The listener is working but I want to extract the value of the entry the user clicks on, not the key of the entire preference. Is there a method for doing that?

1

There are 1 answers

5
Simas On BEST ANSWER

The usual error. You forgot to show the toast:

Toast.makeText(getActivity(), key, Toast.LENGTH_LONG)
        .show();

One of the reasons I stick with Log.e for testing :)

Edit:

findPreference("font_size_preference").setOnPreferenceChangeListener(
        new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object o) {
        Log.e("", "New value is: " + o.toString());
        // True to update the state of the Preference with the new value.
        return true;
    }
});

You can read about the return value in the usual place - The Docs.