Venky Venky - 1 year ago 144
Android Question

spannablestring is not working for programatically createed button

I know SpannableString is possible to set different text size in one textview but if textview is added programatically its not working.

String s = "Best Ever";
SpannableString ss1 = new SpannableString(s);
ss1.setSpan(new RelativeSizeSpan(2f), 0, 4, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 4, 0); // set color
TextView tv = (TextView) findViewById(;

Answer Source

As you can see, It's work on Button and TextView for API17 but work on TextView only on API21

Interesting! I notice that the button on API 21 is all caps..So remove all caps.

By default, Material buttons are styled to show text in all-caps. However, there is a bug in the AllCapsTransformationMethod used for capitalization that causes it to discard Spannable data.

You can override the default button styling by disabling allCaps mode, which is true by default for Material-styled widgets.

From code,


From XML,

    android:textAllCaps="false" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download