Venky Venky - 4 months ago 28
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(R.id.textview);
tv.setText(ss1);

Answer

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,

txt.setAllCaps(false);

From XML,

<View
    ...
    android:textAllCaps="false" />
Comments