onClick event with Android Preferences

0

I have some preferences within the PreferenceScreen in the xml/preferences.xml file. These are some items I need to show to the user. See below:

<Preference
    android:key="pref_key_info"
    android:title="@string/info" />
<Preference
    android:key="pref_key_version"
    android:summary="@string/info_version_sumary"
    android:title="@string/info_version" />

When I click on the first item, in this case the <preference> with key pref_key_info , I want it to open a dialog with some information.

How to put an event of onClick in <preference> ?

    
asked by anonymous 27.03.2017 / 18:50

1 answer

1

First step is to set android:key to the preference item. In this case you already have pref_key_info in the item. Shortly afterwards in its Fragment that extends PreferenceFragment one must create an instance using the findPreference() method. Finally, just use the setOnPreferenceClickListener() method to insert the event by clicking on the specific preference. See below how your code should look.

Preference info = (Preference) findPreference("pref_key_info");
info.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
   @Override
   public boolean onPreferenceClick(Preference preference) {
       // aqui pode inserir a janela do dialogo

       return false;
   }
});

More details on Android Preferences in the documentation .

    
27.03.2017 / 18:53