In this example, I will show you an example of OnPreferenceClickListener
. We will improve our previous article’s preferences XML which was about preference sub-screens. When settings->support->About is clicked, we need to display information about the current app.
Preferences XML
Below is our preferences XML. Note the highlighted PreferenceScreen
which represents the ‘About’ setting. We will add a listener to it so that when clicked, the current app’s information is displayed in form of an alert.
preferences.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:summary="@string/settings_summary" android:title="@string/settings" > <PreferenceScreen android:key="pref_config_settings" android:summary="@string/pref_group_configure_summary" android:title="@string/pref_group_configure" > <PreferenceScreen android:key="pref_appearance" android:title="@string/pref_group_appearance" > <EditTextPreference android:key="pref_list_limit" android:summary="@string/pref_summary_list_size" android:title="@string/pref_list_limit" /> <ListPreference android:defaultValue="@string/pref_color_theme_value" android:dialogTitle="@string/pref_color_theme" android:entries="@array/pref_color_theme_entries" android:entryValues="@array/pref_color_theme_values" android:key="pref_syncConnectionType" android:title="@string/pref_color_theme" /> </PreferenceScreen> <PreferenceScreen android:key="pref_admin_settings" android:title="@string/pref_group_admin" > <CheckBoxPreference android:defaultValue="false" android:key="perf_show_delete" android:title="@string/pref_show_delete" /> </PreferenceScreen> </PreferenceScreen> <PreferenceScreen android:key="pref_support_settings" android:summary="@string/pref_group_support_summary" android:title="@string/pref_group_support" > <PreferenceScreen android:key="@string/pref_support_about" android:title="@string/pref_support_about" /> <CheckBoxPreference android:defaultValue="false" android:key="perf_show_help" android:title="@string/pref_support_show_help" /> </PreferenceScreen> </PreferenceScreen>
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">JavArticles</string> <string name="welcome">Preferences Example</string> <string name="list_size">List Size</string> <string name="settings">Settings</string> <string name="settings_summary">Example of Preferences</string> <string name="pref_show_delete">Show delete button?</string> <string name="pref_list_limit">List Size</string> <string name="pref_summary_list_size">Maximun rows to be shown in a list page</string> <string name="pref_color_theme">Color theme</string> <string name="pref_color_theme_value">#87CEEB</string> <string-array name="pref_color_theme_entries"> <item>Black</item> <item>Sky Blue</item> <item>Red</item> <item>White</item> </string-array> <string-array name="pref_color_theme_values"> <item>#000000</item> <item>#87CEEB</item> <item>#FF0000</item> <item>#FFFFFF</item> </string-array> <string name="pref_group_admin">Admin</string> <string name="pref_group_appearance">Appearance</string> <string name="pref_group_configure">Configuration</string> <string name="pref_group_configure_summary">Configuration of admin, appearance</string> <string name="pref_group_support">Support</string> <string name="pref_group_support_summary">About, Help</string> <string name="pref_support_about">About</string> <string name="pref_support_show_help">Help</string> </resources>
Add preference.onClickPreferenceListener
PreferencesExampleActivity
is our PreferenceActivity
class. Let’s modify it to configure the listener. Using the about item’s key value R.string.pref-support_about
, we will first find its Preference
object. We will call getPreferenceManager().findPreference(key)
where key is the string value of about setting.
We then set the listener using setOnPreferenceClickListener
and pass an object of OnPreferenceClickListener
. In the onPreferenceClick
, we will call the method that will show the about dialog.
PreferencesExampleActivity:
package com.javarticles.android; import android.app.AlertDialog; import android.os.Bundle; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; import android.text.Html; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.widget.TextView; public class PreferencesExampleActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); Preference preference = getPreferenceManager().findPreference( getString(R.string.pref_support_about)); preference .setOnPreferenceClickListener(new OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { showAbout(); return true; } }); } public void showAbout() { StringBuilder aboutText = new StringBuilder(); aboutText .append("") .append(getString(R.string.app_name)) .append(" ") .append(getString(R.string.welcome)) .append(" ") .append("Visit Java Articles for more articles "); Spanned body = Html.fromHtml(aboutText.toString()); TextView textView = new TextView(this); textView.setText(body); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setPadding(5, 0, 5, 0); final AlertDialog.Builder d = new AlertDialog.Builder(this); d.setIcon(android.R.drawable.ic_dialog_info); d.setView(textView); d.setTitle(getString(R.string.pref_support_about)); d.show(); } }
Run the application
Clicking on ‘About’ will open up a dialog showing details of the app.
Download the source code
This was an example of onPreferenceClickListener. You can download the source code here: preferenceListenerExample.zip