Android - ListPreference set summary color fails (with setSummary(spannable))

250 views Asked by At

This is really a weird situation, I set up a normal ListPreference, hoping to set its summary color to another customized color, as I did for all the other preference widgets, like this:

     String mainSummary = listPreference.getSummary().toString();
        Spannable coloredMainSummary = new SpannableString (mainSummary);
        coloredMainSummary.setSpan( new ForegroundColorSpan(Color.RED), 0, 
coloredMainSummary.length(), 0 );
        listPreference.setSummary(coloredMainSummary);

But it didn't work anymore, I can still set the summary to other text that I wanted, but the color won't change as I wanted anymore. Is it because ListPreference doesn't support Spannable? or is there something else that I need to do?

2

There are 2 answers

0
Mark On
public class SpannedSummaryListPreference extends ListPreference {

    private CharSequence mCharSequenceSummary;

    public SpannedSummaryListPreference(Context context) {
        super(context);
        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;
    }
}
0
madrinja On

Maybe it's not meant to work that way i.e by using spans. You can change the color of preference summary through activity style by implementing android:textColorSecondary item