Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting UI preference Summary field to the value of the preference

Tags:

New to Android, I have some code when the user changes a preference I update the Summary field in the UI preference to be the value they entered. However, when the preference activity is created I'd like to set the Summary fields to be the values in the corresponding preferences. Please advise. Thanks.

public class MyPreferenceActivity extends PreferenceActivity implements         OnSharedPreferenceChangeListener {      protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         addPreferencesFromResource(R.xml.preference);         SharedPreferences sp = getPreferenceScreen().getSharedPreferences();         sp.registerOnSharedPreferenceChangeListener(this);     }      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,             String key) {         Preference pref = findPreference(key);         if (pref instanceof EditTextPreference) {             EditTextPreference etp = (EditTextPreference) pref;             pref.setSummary(etp.getText());         }     } } 
like image 603
LiteWait Avatar asked Sep 30 '10 02:09

LiteWait


2 Answers

I am new too so may not be the best code but this is similar to what I am doing. You probably want to register you listener onResume and unregister it onPause though rather than onCreate. I hope this helps.

Mainly you just need to grab the pref, the pref value and set the summary.

public class MyPreferenceActivity extends PreferenceActivity implements         OnSharedPreferenceChangeListener {      protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         addPreferencesFromResource(R.xml.preference);         SharedPreferences sp = getPreferenceScreen().getSharedPreferences();         EditTextPreference editTextPref = (EditTextPreference) findPreference("thePrefKey");         editTextPref                 .setSummary(sp.getString("thePrefKey", "Some Default Text"));     }      protected void onResume() {         super.onResume();         getPreferenceScreen().getSharedPreferences()                 .registerOnSharedPreferenceChangeListener(this);     }      protected void onPause() {         super.onPause();         getPreferenceScreen().getSharedPreferences()                 .unregisterOnSharedPreferenceChangeListener(this);     }      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,             String key) {         Preference pref = findPreference(key);         if (pref instanceof EditTextPreference) {             EditTextPreference etp = (EditTextPreference) pref;             pref.setSummary(etp.getText());         }     } } 
like image 121
Sean Avatar answered Nov 10 '22 03:11

Sean


This worked for me.

public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {     private Preference pref;     private String summaryStr;     String prefixStr;      @Override     protected void onCreate(Bundle savedInstanceState)     {        super.onCreate(savedInstanceState);        addPreferencesFromResource(R.xml.prefs);         SharedPreferences sharedPref = getPreferenceScreen().getSharedPreferences();        sharedPref.registerOnSharedPreferenceChangeListener(this);           }      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)     {         //Get the current summary         pref = findPreference(key);         summaryStr = (String)pref.getSummary();          //Get the user input data         prefixStr = sharedPreferences.getString(key, "");          //Update the summary with user input data         pref.setSummary(summaryStr.concat(": [").concat(prefixStr).concat("]"));     } }    
like image 29
mdacosta Avatar answered Nov 10 '22 04:11

mdacosta