Search code examples
androidtextviewspanned

How do I get the size of text at a specified index of a TextView?


I'm using a TextView that has a Spannable object as the text and has multiple different text sizes. I want to know how do I get the text size at a specified index. If I use the TextView.getPaint() and get the text size from there, it always uses the "default" size, which is just the text size that the textview is set to.


Solution

  • Figured it out. I used this code:

    Spanned span = (Spanned) getText();
    RelativeSizeSpan[] sizeSpans = span.getSpans( charStart, charEnd, RelativeSizeSpan.class );
    

    If this array is not null, it gives a scalar from the default text size by calling getSizeChange(), which I just multiply by the default text size to get my real text size.