Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spannablestring is not working for programmatically created button

I know SpannableString is possible to set different text size in one textview but if textview is added programmatically it's 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); 
like image 202
Venky Avatar asked Apr 24 '15 09:04

Venky


1 Answers

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 ( bug details )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" />
like image 131
King of Masses Avatar answered Nov 04 '22 16:11

King of Masses