How to update a preference summary in android

3.8k views Asked by At

In settings page of my app there are two EditTextPreference (s) When I change the value, the summary has to be updated. Right now it updates the summary as soon as I enter the values in the EditTextPreference (s) and hit OK. But when I restart the application the summary changes back to the default value. But the new value stays in the EditTextPreference (I can see when I open it). The summary gets updated again when I enter a new value in the EditTextPreference. How to make the summary updated from the user input every time ?

Here is my code :

SettingActivity.java

public class SettingsActivity extends AppCompatActivity {


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

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment()).commit();

    setTitle("Settings");
    ActionBar actionBar=getSupportActionBar();
    // Show the Up button in the action bar.
    actionBar.setDisplayHomeAsUpEnabled(true);
}



public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

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

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

    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);

    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);

    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Preference pref = findPreference(key);
        if (pref instanceof EditTextPreference) {
            EditTextPreference listPref = (EditTextPreference) pref;
            pref.setSummary(listPref.getText());
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            startActivity(new Intent(getActivity(), HomeActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
}

settings_layout.xml

 <EditTextPreference
        android:capitalize="words"
        android:dependency="email_switch"
        android:defaultValue="@string/default_email_address"
        android:key="example_text"
        android:summary="@string/pref_description_email"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="@string/pref_email_address_text_title"
        android:inputType="textWebEmailAddress" />
 <EditTextPreference
        android:capitalize="words"
        android:dependency="sms_switch"
        android:defaultValue="@string/default_mobile_number"
        android:key="example_text1"
        android:summary="@string/pref_description_sms"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="@string/pref_mobile_number_text_title"
        android:inputType="phone"/>

P.S. I call this in my MAIN Activity

PreferenceManager.setDefaultValues(this, R.xml.settings_layout, false);
1

There are 1 answers

2
Nick Friskel On BEST ANSWER

In your onCreate() method of the SettingsFragment class, you need to get a reference to your SharedPreferences and then run the onSharedPreferenceChanged() method. Your onCreate() should look like this:

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

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

    //if you are using default SharedPreferences
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());

    onSharedPreferenceChanged(sharedPrefs, getString(R.string.key1));
    onSharedPreferenceChanged(sharedPrefs, getString(R.string.key2));

}

Where key1 and key2 are your preference keys (example_text and example_text1 in your case) saved into your strings file. I had an issue when doing this myself without string references, which is why I recommend to extract them.