Knowing the TextView size in sp through

android: textSize = "XXsp"

Is it possible to calculate what height it will have in px on the device under test in advance? android:layout_height="wrap_content" , no paddings, android: includeFontPadding = "false"

UPD Tested on several emulators and phone, it looks like the formula looks like this:

 height = ОКРУГЛВВЕРХ(textSize * scaledDensity * (7/6)) 

I do not know just whether it will work on all devices.

    5 answers 5

    if after the text is added to the TextView , you can find out in this way

     int totalLineCount = textView.getLineCount(); int lineHeight = textView.getLineHeight(); int totlaHeight = totalLineCount * lineHeight; 
    • Good way. But still it is not clear how getLineHeight () is calculated. For example, for textSize = 30sp, height = 71px on my device, with a density of 2. - iamtihonov
    • there is still padding font is taken and a couple more things :) - andreich
    • can you give more details? I tried to look in source codes, but in them nothing to understand. - iamtihonov
     float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity; int px = scaledDensity*sp; 
    • This will get the font size in px, but not the height of the TextView. The height is greater. - iamtihonov
    • @iamtihonov So it depends not only on textSize, but also on layout_height, padings and margins. If you know their meaning, then just add to it. - Tolyas
    • android: layout_height = "wrap_content", there are no padings and margins. I also thought that then it should be the same value, but after testing it turned out that TextView is higher - iamtihonov
    • @iamtihonov Also depends on the background. It can be plugged in styles. Both standard and custom. Isn't it easier to get a value through getHeight ()? - Tolyas
    • So I do, yes, perhaps the standard background adds the height of TextView or such a realization for it. I wonder if you can find out in advance. - iamtihonov

    Is there such an option, have you tried?

     ViewTreeObserver viewTreeObserver = textView.getViewTreeObserver(); viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int width = textView.getWidth(); int height= textView.getHeight() textView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } }); 
    • Why contact this listener, any widget will return its dimensions through getters. - pavlofff
    • I called getHeight () when testing on click view, this method is not necessary. - iamtihonov

    Thus, you can get the size of the text written in the font size you want:

     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG); paint.setStyle(Paint.Style.FILL); paint.setColor(color); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(textSize); //размер шрифта в **пикселях** Rect bounds = new Rect(); String text = "your text here"; paint.getTextBounds(text , 0, text.length(), bounds); int height = bounds.height();//высота int width = bounds.width();//ширина 
    • You need to calculate what the height of the TextView widget will be, knowing the size of the text in the sp before this text is displayed. - pavlofff

    Directly will not work?

     textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int height = textView.getMeasuredHeight(); int width = textView.getMeasuredWidth(); 
    • We receive this value after the fact, just it needs to be calculated in advance, before display. - iamtihonov
    • then, something like this: int measuredHeight = (new StaticLayout (measuredText, paint, targetWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true)). getHeight (); - georgehardcore