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
}