How can I set the android preference summary text color?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 23:55:25
Sean

OK what I ended up doing was using a Spannable. This takes the color as an integer.

Spannable summary = new SpannableString("Currently This Color");
summary.setSpan(new ForegroundColorSpan(color), 0, summary.length(), 0);
preference.setSummary(summary);
malcie

Use Html.fromHtml to style your text.

mPodFolderPref.setTitle(Html.fromHtml("<font color='red'>" +  mPodFolderPref.getTitle() + "</font>"));
mPodFolderPref.setSummary(Html.fromHtml("<font color='red'>" +  mPodFolderPref.getSummary() + "</font>"));

Html.fromHtml can do a lot for you.

A bit late, but I found useful to write these self-contained methods:

private void setColorPreferencesTitle(EditTextPreference textPref, int color) {
    CharSequence cs     = (CharSequence) textPref.getTitle();
    String plainTitle   = cs.subSequence(0, cs.length()).toString();
    Spannable coloredTitle = new SpannableString (plainTitle);
    coloredTitle.setSpan( new ForegroundColorSpan(color), 0, coloredTitle.length(), 0 );
    textPref.setTitle(coloredTitle);
}

private void resetColorPreferencesTitle(EditTextPreference textPref) {
    CharSequence cs     = (CharSequence) textPref.getTitle();
    String plainTitle   = cs.subSequence(0, cs.length()).toString();
    textPref.setTitle(plainTitle);
}

All the above ways didn't help me. I ended up by extends the Prefernces class:

public class CustomListPreferences extends Preference {

    public CustomListPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomListPreferences(Context context) {
        super(context);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
       ((TextView)view.findViewById(android.R.id.summary)).setTextColor(getContext().get Resources().getColor(R.color.green));
    }

}

Hi you can change the color of preference using Html.fromHtml().

ex : private String title = "" + "Set the SMS Send Limit" + "";

and add set this string from your android application like this .

CheckBoxPreference _test = (CheckBoxPreference)findPreference("text"); _test .setTitle(Html.fromHtml(title ));

follow this link for html view of android : http://www.androidpeople.com/tag/html-tags/

Thanks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!