How to set an edittext preference summary and make it stick

I came back and the fourth about this, and I just can't get it. I customize my settings using a fragment of preferences. I can make the settings work, and I can even get a β€œresume” for the update when I made the change. But if I leave the settings screen and return to it, the summary will revert to the default text. So the question is about using edittext preferences. How do you update the bulletin to show that the user has changed the setting and make it stick to closing the screen and application? In this case, when my users change mse_ip, the summary changes to "MSE IP xxxx", but as soon as I leave the settings screen and return, it returns to "0.0.0.0", which is set for @ string / mseip.

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); } } 
+9
android android-edittext summary
source share
3 answers

You should set OnPreferenceChangeListener to your preference. Soon, every change in preference that calls setSummary changes the resulting display. This could be a sample 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 must also call your setSummary method for preferences in onCreate (), so your SharedPreference is displayed in the Summary.

+2
source share

It seems that you are just updating the value of this EditText. Try to save the new value in the general settings by calling

sharedPreferences.edit().putString(key, editTextPreference.getText()).apply();

This can be called in the updatePreference(String key) function.

0
source share

Using the androidx preference library (see the official manuals ), just add the following attribute:

 <EditTextPreference ... app:useSimpleSummaryProvider="true" /> 
0
source share

All Articles