I am able to show EditTextPreference with default summary. Now i want to update it when i enter new value in Edit Text box and click ok then it should be upadate the value of summary. But i am unable to do that. My code is below please update in my code which required.
public class Prefs extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
final EditTextPreference pref = (EditTextPreference)findPreference("username");
pref.setSummary(pref.getText());
pref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
final EditTextPreference pref = (EditTextPreference)findPreference("username");
pref.setSummary(pref.getText());
return true;
}
});
}
}
xml file is
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:title="User Name"
android:key="username"
android:summary="Please provide user name" />
<EditTextPreference
android:title="Password"
android:password="true"
android:key="password"
android:summary="Please enter your password" />
</PreferenceScreen>
actually my code update previous enter value as summary text. How to show text as summary when I click ok button. Thanks
You can implement this simply by subclassing EditTextPreference
and overriding getSummary()
.
package com.example.yourapplication;
import android.content.Context;
import android.preference.EditTextPreference;
import android.text.TextUtils;
import android.util.AttributeSet;
public class FriendlyEditTextPreference extends EditTextPreference {
public FriendlyEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public FriendlyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FriendlyEditTextPreference(Context context) {
super(context);
}
// According to ListPreference implementation
@Override
public CharSequence getSummary() {
String text = getText();
if (TextUtils.isEmpty(text)) {
CharSequence hint = getEditText().getHint();
if (!TextUtils.isEmpty(hint)) {
return hint;
} else {
return super.getSummary();
}
} else {
CharSequence summary = super.getSummary();
if (!TextUtils.isEmpty(summary)) {
return String.format(summary.toString(), text);
} else {
return summary;
}
}
}
}
In your xml, replace EditTextPreference
with com.example.yourapplication.FriendlyEditTextPreference
Now you can use string containing "%s" or "%1$s" as android:summary
and it will update automatically; moreover, you can set andorid:hint
so that the hint is also displayed when text is empty.
For A working ListPreference
on all platforms, see my answer of this uestion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With