Search code examples
androidtextviewmaterial-design

Android TextView making the font different size per line


I want to use a TextView

The first line, I want the font to be 24dp the second line, I want the font to be 16dp

Could this be done with 1 TextView or will I need to create 2 of them?


Solution

  • It can be done in 1 TextView with SpannableString

    From https://developer.android.com/reference/android/text/style/RelativeSizeSpan

    You can use:

    SpannableString string = new SpannableString("Text with relative size span");
    string.setSpan(new RelativeSizeSpan(1.5f), 10, 24, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    

    Where 1.5f would be an increase of 50%, 10 the starting index and 24 the end index

    public void setSpan (Object what, 
                    int start, 
                    int end, 
                    int flags)
    

    Another example here Different font size of strings in the same TextView too