Inspired by how-do-i-display-the-current-value-of-an-android-preference-in-the-preference-summary i created my own EditTextPreference that shows its current value in the PreferenceScreen.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen android:key="preferences" xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="First Category"
android:key="first_category">
<de.k3b.widgets.EditTextPreferenceWithSummary
android:key="test"
android:title="Test Message"
android:dialogTitle="Test Message"
android:dialogMessage="Provide a message"
android:defaultValue="Default welcome message" />
</PreferenceCategory>
</PreferenceScreen>
The implementation looks like this
package de.k3b.widgets;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.*;
import android.util.AttributeSet;
import android.util.Log;
public class EditTextPreferenceWithSummary extends EditTextPreference {
private final static String TAG = EditTextPreferenceWithSummary.class.getName();
public EditTextPreferenceWithSummary(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public EditTextPreferenceWithSummary(Context context) {
super(context);
init();
}
private void init() {
Log.e(TAG, "init");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());
String currentText = test prefs.getString("minTrashholdInSecs", this.getText());
// where do i get the current value of the underlaying EditTextPreference ??
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
this.setSummary(this.getText());
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.w(TAG, "display score changed to "+newValue);
preference.setSummary(newValue.toString()); // getSummary());
return true;
}
});
}
}
When the PreferenceScreen is first shown there is no current value displayed.
My problem: Where do i get the current value that the EditTextPreference represents? getText() does not get the value as i expected. After changing the preferencevalue this value is shown in the summary field as expected.
Im am using android 2.2