0

Im using a TextView with the property autoSizeTextType="uniform".

The font size its ok, but now, I need to get the current size, but Im always getting "328", so this size does not correspond with the displayed size.

Any idea of how to get the size?

Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
  • check this https://stackoverflow.com/questions/37789282/how-to-auto-size-textview-dynamically-according-to-the-length-of-the-text-in-and – Wini Mar 02 '20 at 12:53
  • Thanks but this question is different, I need to get the value like "getFontSize" using "autoSizeTextType" – Pablo Cegarra Mar 02 '20 at 12:57

1 Answers1

0

If you look at the getTextSize(), it returns the text size in pixel so If you want the text size in sp(sp is the most commonly used textSize unit) then you can use the method given below.

public static int getSPfromPX(int px) {
        int sizeValue;
        float sp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, px, getResources().getDisplayMetrics());
        sizeValue = (int) Math.ceil(sp);
        return sizeValue;
    }

And here is it's Kotlin variant

fun getSPfromPX(px: Int): Int {
        val sizeValue: Int
        val sp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, px.toFloat(), resources.displayMetrics)
        sizeValue = ceil(sp.toDouble()).toInt()
        return sizeValue
    }
Mittal Varsani
  • 5,601
  • 2
  • 15
  • 27
  • This not works using autosize, the textview returns a size different that the displayed text (always the same) using 100 characters or 10, I hope I have explained this as best I can. – Pablo Cegarra Mar 02 '20 at 12:47