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()); } } }
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()); } } }
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("]")); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With