1
votes

I know in Android that you can scale the images based on screen densities. For instance, if mdpi is the baseline,

  • hdpi = 1.5 * mdpi
  • xhdpi = 2 * mdpi
  • xxhdpi = 3 * mdpi
  • xxxhdpi = 4 * mdpi.

Are there similar values for font size?

4
Font sizes in Android adjust automatically based on the density of the screen and user font settings if you use 'sp'PPartisan

4 Answers

2
votes

what you need to do is to perform a conversion using the scaleDensity (here the documentation) value, member of the DisplayMetrics class. From the documentation

A scaling factor for fonts displayed on the display. This is the same as density, except that it may be adjusted in smaller increments at runtime based on a user preference for the font size.

0
votes

Exactly the same values might be used for font size

android:textSize="18dp"

Although it's recommended to use sp (scale independent pixels). It's almost the same thing as dp, but it might scale-up based on user accessibility settings (for instance, if you haw low vision, you can scale-up font size affecting how sp is calculated). So, in the end it becomes:

android:textSize="18sp"

Once again, if you're using default settings, 1dp == 1sp

0
votes

If you want to do it programmatically:

view.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
0
votes
  1. Create these 5 folders in your res folder
    values-ldpi
    values-mdpi
    values-hdpi
    values-xhdpi
    values-xxhdpi

  2. And add dimens.xml in each folder.
  3. Paste this code in each dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="font_size">72sp</dimen>
</resources>

Depending of Screen density change the font size.