Getting default text size in pixels in Android

I need to know the pixel size of the current Android font by default. I copied the approach indicated in a very similar question here . However, instead of getting the size in pixels, I always get -1. Any ideas what I'm doing wrong?

Here is the code:

Context context = getActivity(); TypedValue typedValue = new TypedValue(); context.getTheme().resolveAttribute(android.R.attr.textAppearance, typedValue, true); int[] textSizeAttr = new int[] { android.R.attr.textSize }; TypedArray a = context.obtainStyledAttributes((AttributeSet) typedValue.string, textSizeAttr); textSize = a.getDimensionPixelSize(0, -1); Log.e("Metrics", "text size in dp = " + String.valueOf(textSize)); a.recycle() 
+4
source share
2 answers

It is very simple to get the text size of the textView here is the code

 textView.getTextSize(); 

Returns the size (in pixels) of the default text size in this TextView

+5
source

Just change this line of code

 TypedArray a = context.obtainStyledAttributes((AttributeSet) typedValue.string, textSizeAttr); 

To

  TypedArray a = context.obtainStyledAttributes(typedValue.data, textSizeAttr); 
+1
source

All Articles