13

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); 
Cœur
  • 37,241
  • 25
  • 195
  • 267
Venky
  • 166
  • 1
  • 6

1 Answers1

32

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" />
King of Masses
  • 18,405
  • 4
  • 60
  • 77
  • 2
    Thanks! was about to bang my head! – Muhammad Babar Jan 12 '16 at 08:02
  • please update your answer because no `android:allCaps` attribute. I've found that `android:textAllCaps` is the right one. – ישו אוהב אותך Jul 30 '16 at 04:13
  • @isnotmenow thanks for initiating, i updated my answer :) – King of Masses Jul 30 '16 at 08:45
  • 1
    I have been trying to figure this out for a day now. I knew it would be something silly like this. In my case, I was trying to make the decimal for a number 50% smaller. Who would've thought you can capitalize a number in Android! – Cliff Gunn Oct 20 '16 at 20:07
  • What's the source for the mentioned `AllCapsTransformationMethod` bug that discards the `Spannable` data? Is there one? – Mokkun Aug 28 '19 at 06:24
  • 1
    This is a known bug with the textAllCaps attribute, specifically with AllCapsTransformationMethod. http://code.google.com/p/android/issues/detail?id=67509 @もっくん – King of Masses Aug 28 '19 at 07:02