9

I want to create a button. This button contains a letter with a size of 22pixels and some letters to the right of it with a size of 16 pixels.

Like this:
enter image description here

How can I achieve this?

At the moment I have this:

private void setText(Button btn, String text, String underText) {
    Spannable span = new SpannableString(text + "\n" +  underText);

    btn.setText(span);
}
Robby Smet
  • 4,649
  • 8
  • 61
  • 104
  • Lock at this http://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a-textview – Pasha Aug 30 '12 at 09:31

2 Answers2

19

You can apply AbsoluteSizeSpan in your Spannable to make different size of Text in same content.

 private void setText(Button btn, String text, String underText) {

    Spannable span = new SpannableString(text + "\n" +  underText);
    span.setSpan(new AbsoluteSizeSpan(fontSize), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    btn.setText(span);
}
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
7

Instead of doing it through code, you may simply achieve it by using html stuff in string resource. For example:

Define a string resource:

<string name="tmp"><font size="30">2</font>abc</string>

and apply it to your button's text:

android:text="@string/tmp"
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • 1
    i believe dimensions related to html are in pixels – waqaslam Mar 27 '13 at 15:24
  • While this works it is incredibly slow... if you're updating the text often or have lot of these cases in your view hierachy, don't use it SpannabelStringBuilders API might not be pretty, but it's fast – martyglaubitz May 22 '15 at 11:48