28

I have a simple Button:

<Button
    android:id="@+id/test"
    android:textColor="@color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

and try to change text property by:

SpannableString span = new SpannableString(text);
span.setSpan(new AbsoluteSizeSpan(8, true), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
testButton.setText(span);

It works on Android 4.3 but doesn't on 5.0.

The interesting thing is when I change implementation from Button to TextView it works fine on 5.0. Seems to be something with Button in Lollipop.

klimat
  • 24,711
  • 7
  • 63
  • 70
  • "It works with Android 4.3, but doesn't with 5.0." -- please define what "doesn't" means here. If you mean that your `AbsoluteSizeSpan` is lost, that does not shock me, as Android 5.0 seems to want to control the captions more, such as forcing them to all caps. – CommonsWare Mar 12 '15 at 10:54
  • @CommonsWare yes, span is ignored. – klimat Mar 12 '15 at 10:55
  • I would expect that there are many developers who are irritated with Android 5.0's default of all-caps for the caption. If there is a workaround for blocking that behavior, it might also allow your `AbsoluteSizeSpan` to continue working. – CommonsWare Mar 12 '15 at 10:56
  • You can turn off all caps. Search on Google or SO. – alanv Mar 12 '15 at 17:50

1 Answers1

90

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 and disable all-caps by specifying android:textAllCaps="false" on your Button.

<Button
    ...
    android:textAllCaps="false" />
alanv
  • 23,966
  • 4
  • 93
  • 80
  • 24
    Another wtf Android moment...when you have TextView with setAllCaps set to true, Spannables stops working. In my case was a ForegroundColorSpannable. Thank you for answer. – Michal Jul 18 '15 at 15:48
  • For more discussion, please see: http://stackoverflow.com/questions/32257606/imagespan-not-working-on-android-5/32867216?noredirect=1#comment53573775_32867216 – i_am_jorf Sep 30 '15 at 16:36