I am creating a preference screen that has a few EditTextPreference
and ListPreference
attributes. I am trying to get the summary text to display " not set " when the preference is not set. Using etp.setSummary(Html.fromHtml("<i> not set </i>")
works for EditTextPreference
attributes but lp.setSummary(Html.fromHtml("<i> not set </i>")
doesn't work for ListPreference
attributes. The text "not set" is displayed but not in italics.
I have tried using the following to format the summary:
ListPreference lp = (ListPreference)findPreference("def_list_pref");
SpannableString str = SpannableString.valueOf("not set");
str.setSpan(new StyleSpan(Typeface.ITALIC), 0, str.length(), 0);
lp.setSummary(str);
and I've also tried manually setting the summary in preference.xml for ListPreference attributes to android:summary="<i> not set </i>"
but that didn't work either.
Does anyone have any idea what I could be doing wrong or is this a bug with the ListPreference
?
Thanks.
public class SpannedSummaryListPreference extends ListPreference {
private CharSequence mCharSequenceSummary;
public SpannedSummaryListPreference(Context context) {
this(context,null);
}
public SpannedSummaryListPreference(final Context context, final AttributeSet attrs) {
super(context,attrs);
mCharSequenceSummary = getSummary();
}
@Override
public CharSequence getSummary() {
final CharSequence entry = getEntry();
if (super.getSummary() == null || entry == null) {
return super.getSummary();
} else {
if (mCharSequenceSummary instanceof String) {
return super.getSummary();
} else {
return mCharSequenceSummary;
}
}
}
@Override
public void setSummary(CharSequence summary) {
super.setSummary(summary);
mCharSequenceSummary = summary;
}
}`
EDIT: Kotlin solution:
class ListPreferenceEx @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ListPreference(context, attrs) {
private var mCharSequenceSummary: CharSequence? = null
init {
mCharSequenceSummary = summary
}
override fun getSummary(): CharSequence? {
val entry = entry
val superSummary = super.getSummary()
return if (superSummary == null || entry == null || mCharSequenceSummary is String)
superSummary
else
mCharSequenceSummary
}
override fun setSummary(summary: CharSequence) {
super.setSummary(summary)
mCharSequenceSummary = summary
}
}
Use Context#getText(int resId) to set summary text on your Preference programmatically :
findPreference("my_pref").setSummary(getText(R.string.settings_network_summary));
With your string resource formatted like this
<string name="html_string" ><u>underline</u> <i>italic</i> <b>bold</b></string>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.