I have a table with a header row coded in the xml file. I set the TextSize
attribute of the TextView inside it to 20dp. Now I want to add textview programmatically with the same textSize, but when I use tv.setTextSize(20)
they are litte bigger.
I tried tv.setTextSize(20 / ((float) getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT));
but that way is too small. What's the correct way to do it?
You have to declare your textview size in dps inside app/res/values/dimen.xml
folder
<dimen name="text_size_20_dp">20dp</dimen>
Java:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
getResources().getDimension(R.dimen.text_size_20_dp));
Kotlin:
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
resources.getDimension(R.dimen.text_size_20_dp))