I have two szenarios:
First:
textView.setTextSize(getResources().getDimension(R.dimen.defaultTextSize));
Second in xml:
android:textSize="@dimen/defaultTextSize"
in values/dimen.xml i have declared defaultTextSize with 20sp
In my first case the text is much bigger (and different in some screenresulutions) than in my second case. Why? Have I done a mistake?
To set Android Button font/text size, we can set android:textSize attribute for Button in layout XML file. To programmatically set or change Android Button font/text size, we can pass specified size to the method Button. setTextSize(specific_size).
Adding fonts to a TextView To set a font for the TextView , do one of the following: In the layout XML file, set the fontFamily attribute to the font file you want to access. Open the Properties window to set the font for the TextView .
setTextSize()
takes unit as pixel
Use this
public float pixelsToSp(float px)
{
float scaledDensity = _context.getResources().getDisplayMetrics().scaledDensity;
return px/scaledDensity;
}
textView.setTextSize(pixelsToSp(getResources().getDimension(R.dimen.defaultTextSize)));
or
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.defaultTextSize))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With