25

On my preference screen I have a preference that when clicked opens a color picker dialog. What I would like to do is when the user selects a color, that the text summary of the preference is displayed in that color.

I know I can have the summary set up like this, Currently <font color="#ff0000">this color</font> and have it display in that color. The problem is the color I am getting back is the android int color.

I could use the red(), green(), blue() methods and then convert those to Hex and then combine them into a string so I could set the summary text with the new value and that works: String colorString = String.format("#%02x%02x%02x",Color.red( defaultColor ), Color.green( defaultColor ), Color.blue( defaultColor )); I was just curious if there is an easier way to do this.

Thanks ahead of time.

Sean

Sean
  • 1,278
  • 1
  • 12
  • 11

5 Answers5

36

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);
Roman Holzner
  • 5,738
  • 2
  • 21
  • 32
Sean
  • 1,278
  • 1
  • 12
  • 11
6

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.

chopper
  • 6,649
  • 7
  • 36
  • 53
Mal Clarke
  • 147
  • 2
  • 8
2

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));
    }

}
yshahak
  • 4,996
  • 1
  • 31
  • 37
2

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);
}
tos
  • 996
  • 1
  • 10
  • 20
-1

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

Sujit
  • 10,512
  • 9
  • 40
  • 45
  • 2
    I am aware of that. However, like I said above, I have access to the color as an int which is how Android stores it. Not the RGB or ARGB strings. – Sean Oct 28 '10 at 20:42