How do I measure the height of a SpannableString?

I have a TextView in which I am showing a SpannableString with different Span sizes. I need to know the exact height of the last word in the SpannableString, but the method I am using is wrong. What am I doing wrong?

Here is the test code I am using

    SpannableStringBuilder spannableString = new SpannableStringBuilder("Some Text Some Text Some Text Some Text Some Text Some Text Some Text Some Text Some Text");
    spannableString.setSpan(new AbsoluteSizeSpan(10, true), 0, spannableString.length() / 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new AbsoluteSizeSpan(15, true), spannableString.length()/3, 2 * spannableString.length()/3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(new AbsoluteSizeSpan(20, true), 2 * spannableString.length() / 3, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    surfaceView.setTranslationY(measureSpannableHeight());

private int measureSpannableHeight() {
    StaticLayout staticLayout = new StaticLayout(spannableString, new TextPaint(), 480, Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
    return staticLayout.getHeight();
}

      

(surface conversion is considered only to illustrate the height measurement result)

+3
android text spannablestring


source to share


No one has answered this question yet

Check out similar questions:

4800
How to find all files containing specific text in Linux?
3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
2510
How to persist android activity state by persisting instance state?
2284
How can I fix 'android.os.NetworkOnMainThreadException'?
1844
How to place text horizontally and vertically in a TextView?
3
Measuring text width
1
How to enable clickable text in staticlayout and viewview
1
TextView with anchor and raw links (SpannableString issue)
0
Finding the width / height of an element on the canvas without pulling it to the screen
0
Android: Add words to SpannableStringBuilder?



All Articles
Loading...
X
Show
Funny
Dev
Pics