SpannableString on button not works for API21?

android, android-5.0-lollipop, spannablestring

Solution

You can work around this issue by disabling `allCaps` mode, which is `true` by default for Material-styled buttons.

From code,

txt.setAllCaps(false);

From XML,

<Button
    ...
    android:textAllCaps="false" />

Problem

I try to put an icon on the toggle button by using SpannableString. and working on API17 but not on API21. As you can see, It's work on Button and TextView for API17 but work on TextView only on API21 (The 'Z' is not replace by icon). ``` private SpannableStringBuilder createSpanIcon(int rid, String name){ Drawable d = getActivity().getResources().getDrawable(rid); d.setBounds(0,0,20,20); ImageSpan imageSpan = new ImageSpan(d, DynamicDrawableSpan.ALIGN_BASELINE); SpannableStringBuilder builder = new SpannableStringBuilder(); builder.append("Z\n") .append(name) .setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); /* This give same result builder.append("Z", imageSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) .append("RED", new ForegroundColorSpan(Color.RED), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) .append("\n" + name); */ return builder; } ``` and ``` Button btn = (Button)v.findViewById(R.id.myButton); SpannableStringBuilder _test = createSpanIcon(R.drawable.tc_g, "Click Me!" ); btn.setText( _test ); TextView txt = (TextView)v.findViewById(R.id.myText); SpannableStringBuilder _test2 = createSpanIcon(R.drawable.tc_g, "I'm text" ); txt.setText( _test2 ); ``` Note: I dont want to use `setCompoundDrawablesWithIntrinsicBounds`, be cause I'm gonna put the icons on `ToggleButton` so `setTextOn` and `setTextOff` will swap icons for me. Anyone have an Idea why? and any work around?

Original source