How to set edittext preference summary and have it stick

user3423167 picture user3423167 · Mar 26, 2014 · Viewed 11.8k times · Source

I have gone back and fourth on this and I just can not get it. I am setting up my settings using a preference fragment. I can get the settings to work and I can even get the "summary" to update when I make the change. But if I leave the settings screen and come back to it, the summary is back to the default text. So the question is, when using an edittext preference. How do you update the summary so it shows what the user changed the setting to and make it stick across closing the screen and app? In this case when my users change the mse_ip the summary changes to "MSE IP x.x.x.x" but as soon as I leave the settings screen and come back it is back to "0.0.0.0" which is what @string/mseip is set to.

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">     
    <EditTextPreference
            android:key="mse_ip"
            android:title="MSE IP"
            android:summary="@string/mseip"
            android:defaultValue="0.0.0.0"
            android:dialogTitle="IP Address for mse" />
    <EditTextPreference
            android:key="mse_username"
            android:title="Username"
            android:summary="MSE Username %s"
            android:defaultValue="Admin"
            android:dialogTitle="Username for mse" />
    <EditTextPreference
            android:key="mse_password"
            android:title="MSE Password"
            android:password="true"
            android:summary="******"
            android:defaultValue="Admin"
            android:dialogTitle="Password for mse" />
</PreferenceScreen>

preferencesfragment

package com.hmkcode.android;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;

/*public class PrefsFragment extends PreferenceFragment {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);

  // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
 } 
} */
public class PrefsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    // set texts correctly
    onSharedPreferenceChanged(null, "");

}

@Override
public void onResume() {
    super.onResume();
    // Set up a listener whenever a key changes
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
    super.onPause();
    // Set up a 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("mse_ip")){
                Preference preference = findPreference(key);
                if (preference instanceof EditTextPreference){
                    EditTextPreference editTextPreference =  (EditTextPreference)preference;
                    if (editTextPreference.getText().trim().length() > 0){
                        editTextPreference.setSummary("MSE IP  " + editTextPreference.getText());
                    }else{
                        editTextPreference.setSummary("MSE IP Not");
                    }
                }
            }
        }

}

setpreferenceactivity

import android.os.Bundle;
import android.app.Activity;

public class SetPreferenceActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction().replace(android.R.id.content,
            new PrefsFragment()).commit();

    //setContentView(R.layout.activity_set_preference);
}
 }

Answer

Nick picture Nick · Mar 16, 2015

You have to set an OnPreferenceChangeListener to your Preference. So on every preference change, calling setSummary, changes the summary display. This might be an example code:

final Preference pref = getPreferenceManager().findPreference(
                PREF_KEY);
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                pref.setSummary(newValue.toString());
                return true;
            }
        });

You should also call your preference's setSummary method in onCreate(), so that Summary displays your sharedPreference value.