Inconsistency when setting TextView font size in code and in resources
You should use setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
because the documentation of the getDimension
method states that it returns a Resource dimension value multiplied by the appropriate metric.
which I understand to be the precalculated absolute px value.
That is, use:
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_size_small));
Somehow this seems to fit:
XML:
<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="typo14">9sp</dimen></resources>
Java:
setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.typo14));