Event onClick with Android Preferences

Asked

Viewed 54 times

0

I have some preferences within the PreferenceScreen in the archive xml/preferences.xml. These are some items that I need to be shown 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 you click on the first item, in case the <preference> keyed pref_key_info, I want you to open a dialog screen with some information.

How to place an event onClick in the <preference>?

1 answer

1


First step is to define the android:key for the preference item. In this case you already have the pref_key_info item. Right after in your Fragment which extends the PreferenceFragment an instance should be created using the method findPreference(). Finally, just use the method setOnPreferenceClickListener() 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 about Android Preferences in the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.