NullPointerException
NullPointerException

Reputation: 37627

How to set text size depending on the screen dimensions?

I need to set all the text content of the application depending on the screen dimensions. I mean buttons, textviews etc... For example, If i execute the app ina Pixel C tablet, the buttons text size and the textviews text size must be very big, and very small in a 320x240 device.

First I tried autoSizeTextType but finally I discart this approach because it can only be applyed to TextView and not to Buttons, etc...

After that, I tried this approach: https://developer.android.com/training/multiscreen/screendensities creating 4 folders inside res folder:

values-hdpi
values-mdpi
values-xhdpi
values-xxhdpi

And inside them a dimens.xml file with the text size values, for example, this is the file inside the values-xhdpi folder:

<resources>
    <dimen name="big_text">35sp</dimen>
    <dimen name="medium_text">25sp</dimen>
    <dimen name="small_text">20sp</dimen>
</resources>

And for the other folders i put different values to test it.

It works... but... is not the behaviour I'm searching for. Why? Because I executed the application in two different xhdpi emulators, the Pixel C and the Nexus 4. Pixel C has bigger resolution, but the screen is super big, so both devices uses the same dimens.xml file, but the buttons and texts are being displayed small in Pixel C and super big in Nexus 4, as you can see in this picture:

enter image description here

What should I do to get text size value which occupies the same portion of the screen for a phrase in all the devices?

Upvotes: 1

Views: 2600

Answers (1)

Ben P.
Ben P.

Reputation: 54204

You're on the right path, but you should use the smallest width or available width qualifiers rather than density qualifiers. As you have found, the pixel density of a display does not actually have any bearing on the physical dimensions of that display.

The smallest width qualifier is the "normal" way to differentiate screen sizes. "Smallest width" gives you guarantees about the size of the device regardless of its current orientation.

values/           <-- fallback for everything else
values-sw400dp/   <-- modern "large" phones
values-sw600dp/   <-- 7" tablets
values-sw720dp/   <-- 10" tablets

The available width qualifier, on the other hand, gives you guarantees about the size of the device for the current orientation. That is, a phone in portrait will have a much smaller available width than a phone in landscape (whereas it would have the same smallest width value for both portrait/landscape).

values/          <-- fallback for everything else
values-w400dp/   <-- any device that currently has 400dp or more available width
values-w600dp/   <-- 600dp or more available width
values-w720dp/   <-- 720dp or more available width

Again, the real question is whether you want to use the same text sizes for different orientations. If yes, use smallest width. If no (you want different sizes for different orientations), use available width.

Upvotes: 1

Related Questions