I have variable at dimens.xml
<resources>
<dimen name="btn_text_size">12sp</dimen>
</resources>
And i can use it in layout file:
<TextView
android:textSize="@dimen/btn_text_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_tags_complete"
/>
or programmatically
tagButton.setTextSize(c.getResources().getDimension(R.dimen.tag_text_size));
But this 2 methods give different results. I know that getDimension
are based on the current DisplayMetrics associated with the resources.
But what should i do to make this 2 ways looks the same?
setTextSize( float )
expects a scaled pixel value. So, setTextSize( 12 )
would give you the desired result. However, getDimension()
and getDimensionPixelSize()
return the size in units of pixels, so you need to use the unit-typed variant of setTextSize()
as follows:
setTextSize( TypedValue.COMPLEX_UNIT_PX, getDimensionPixelSize( R.dimen.tag_text_size ) );