Display Image in ListPreference Summary

I am trying to add images on ListPreferenceand I have achieved this, but on Android API level 15 or higher it does not work. What am I doing wrong? I tested this code on 2.2 and 2.3.3 and everything works fine! Here is my code.

private void addSummary(Drawable d, Spannable sp) {

    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(Const.IMAGE_ANCHOR);
    builder.append("  ");
    builder.append(sp);
    setTextColor(builder);
    ImageSpan imageSpan = new ImageSpan(d);
    int end = Const.IMAGE_ANCHOR.length();

    builder.setSpan(imageSpan, 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    listPreference.setSummary(builder);

}
+5
source share
1 answer

Instead of using this approach, a cleaner approach would be to declare a layout for the preference object and set the android:layoutpreference option with the resource identifier of your custom layout.

Here is an example of working code.

+2
source

All Articles