The easiest way is to define your font sizes in your resources with the units of scale-independent pixels (sp
) — this unit is like density independent pixels (dp
or dip
) in that it takes into account the screen density but it also takes into account the font-size setting of the user.
To add a new dimension create a dimens.xml
file in your res/values
folder and enter the following code to add a new dimension with the name myFontSize
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="myFontSize">20sp</dimen>
</resources>
You can then get the text size in your application using
int scaledSize = getResources().getDimensionPixelSize(R.dimen.myFontSize);
The resulting size will be correctly scaled to take into account the current screen density and font-size setting.
For more information see the Android Developers page on More Resources.