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);
In your
onCreate()
method of theSettingsFragment
class, you need to get a reference to yourSharedPreferences
and then run theonSharedPreferenceChanged()
method. YouronCreate()
should look like this:Where
key1
andkey2
are your preference keys (example_text
andexample_text1
in your case) saved into yourstrings
file. I had an issue when doing this myself without string references, which is why I recommend to extract them.