Search code examples
androidbuttontext-formatting

Different textsizes in a button


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);
}

Solution

  • 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);
    }