Skip to content

Instantly share code, notes, and snippets.

@jezinka
Created Apr 16, 2017
Embed
What would you like to do?
public class SettingsActivity extends PreferenceActivity {
final static String PREFS_FIRST_DAY = "PREFS_FIRST_DAY";
final static String PREFS_DAYS_NO = "PREFS_DAYS_NO";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
SharedPreferences.OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
Preference pref = findPreference(key);
if (pref instanceof EditTextPreference) {
EditTextPreference editTextPreference = (EditTextPreference) pref;
pref.setSummary(editTextPreference.getText());
}
MainActivity.preferenceChanged = true;
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
addPreferencesFromResource(R.xml.preferences);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment