Reputation: 14755
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
Upvotes: 2
Views: 3076
Reputation: 3833
Here is a more complete example, that includes when the value updates, without having to install preference listeners.
import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class EditTextPreferenceWithValueSummary extends EditTextPreference{
public EditTextPreferenceWithValueSummary(Context context) {
super(context);
}
public EditTextPreferenceWithValueSummary(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected View onCreateView(ViewGroup parent) {
this.setSummary(this.getText());
return super.onCreateView(parent);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
this.setSummary(getText());
}
}
}
And in your xml/settings.xml
:
<your.package.views.EditTextPreferenceWithValueSummary
android:key="some_preference_key"
android:title="Some Title" />
Upvotes: 2
Reputation: 448
Try to add the following code to your EditTextPreferenceWithSummary
class:
@Override
protected View onCreateView(ViewGroup parent) {
this.setSummary(this.getText());
return super.onCreateView(parent);
}
As for me, it works. I think the problem was that you were trying to change the UI state out of UI thread (in fact in the constructor).
Upvotes: 3