I'm a learning how to develop in Android and want to make a setting activity,
My setting activity
public class Main extends Activity {
protected SettingsFragment settingsFragment;
@SuppressLint("NewApi")
@TargetApi(11)
public class SettingsFragment extends PreferenceFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
setSummaries();
}
@Override
public void onResume() {
final SharedPreferences sh = getPreferenceManager().getSharedPreferences() ;
super.onResume();
sh.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onPause() {
final SharedPreferences sh = getPreferenceManager().getSharedPreferences() ;
super.onPause();
sh.unregisterOnSharedPreferenceChangeListener(this);
}
@SuppressLint("NewApi")
public void setSummaries(){
final SharedPreferences sh = getPreferenceManager().getSharedPreferences() ;
//Pref1
Preference stylePref = findPreference("editTextPref");
stylePref.setSummary(sh.getString("editTextPref", ""));
//here the other preferences..
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("editTextPref")) {
Preference pref = settingsFragment.findPreference(key);
// Set summary to be the user-description for the selected value
pref.setSummary(sharedPreferences.getString(key, ""));
}
//here the others preferences
}
}//End fragment
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settingsFragment = new SettingsFragment();
getFragmentManager().beginTransaction()
.replace(android.R.id.content, settingsFragment)
.commit();
}
}
and my res/preferences.xml
file
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="BTA"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="editTextPref"
android:title="Numero de telephone"
android:summary="This allows you to enter a string"
android:defaultValue="*"/>
</PreferenceCategory>
</PreferenceScreen>
So now i have the activity for the setting activity. But i want to display the value of the EditTextPref in android:summary
.
I have found many topics but all the functions was deprecated.
EDIT : thanks to @Ace_McIntosh , I edited my code for people who want it, it's working now.