In Today’s post we are going to look at How to Change Summary of Edit Text Preference. But before moving ahead, if you want to know more about Preferences click here.
First create a preference screen with only one preference EditTextPreference as show below in xml. Let us call this xml as setttings.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <EditTextPreference android:dialogTitle="Enter Name" android:key="name" android:summary="Enter Your Name" android:title="Edit Text Preference" /> </PreferenceScreen>
As shown above we are creating EditTextPreference with key “name“.
Now create a class called AdvancePreferenceExample which extends PreferenceActivity and implements onSharedPreferenceChangeListener as shown below.
import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceActivity; public class AdvancePreferenceExample extends PreferenceActivity implements OnSharedPreferenceChangeListener{ private static final String KEY_EDIT_TEXT_PREFERENCE = "name"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings); } @Override protected void onResume(){ super.onResume(); // Set up a listener whenever a key changes getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this); updatePreference(KEY_EDIT_TEXT_PREFERENCE); } @Override protected void onPause() { super.onPause(); // Unregister the listener whenever a key changes getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { updatePreference(key); } private void updatePreference(String key){ if (key.equals(KEY_EDIT_TEXT_PREFERENCE)){ Preference preference = findPreference(key); if (preference instanceof EditTextPreference){ EditTextPreference editTextPreference = (EditTextPreference)preference; if (editTextPreference.getText().trim().length() > 0){ editTextPreference.setSummary("Entered Name is " + editTextPreference.getText()); }else{ editTextPreference.setSummary("Enter Your Name"); } } } } }
Implementing onSharedPreferenceChangeListener helps to capture event that occur when value of any of the Preference is changed via method onSharedPreferenceChanged. Method onSharedPreferenceChanged has two parameters:
- Instance of SharedPreferences having the key and value of the Preferences defined in the preferences screen via xml
- Key of the preferences whose value has changed.
We need to register the above class, so as to get onSharedPreferenceChanged method of this class is called whenever shared preference values get changed and this is achieved by following line in onResume method.
getPreferenceScreen().getSharedPreferences() .registerOnSharedPreferenceChangeListener(this);
Similarly we also need to unregister the listener which is done in onPause method.
getPreferenceScreen().getSharedPreferences() .unregisterOnSharedPreferenceChangeListener(this);
Method updatePreference accepts key of the preferences to be updated. This methods first compares the key and if key matches with that of EditTextPreference key, EditTextPreference summary will be updated accordingly. This method is called from onSharedPreferenceChanged method and also for onResume method. Reason for calling it from onResume method is to make sure that summary is processed and updated appropriately even when screen comes up for the 1st time after App as started.
Conclusion
In above post we saw how to capture event raised when value of preferences change and how we can use onSharedPreferenceChanged method and updated summary of EditTextPreference.