1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

1

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.

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • I'm trying your proposal but isn't work, don't know why, but I created a values-sw1600dp folder with a very big text size and is not affecting Pixel X which haves 2600x1800 resolution. Why? – NullPointerException Sep 24 '19 at 17:53
  • In fact I disscovered that all devices are ignoring all the created folders and are reading the default values/dimens.xml file... why? – NullPointerException Sep 24 '19 at 18:03
  • These values are in `dp`, not `px`. For example, the Pixel XL has 1440 physical pixels across, but is only 411dp wide (a scaling factor of 3.5). Like density, the number of physical pixels doesn't indicate size (because you could have a very high-res display or a very low-res display, but both could be 8"). – Ben P. Sep 24 '19 at 19:17
  • Almost no device will have a smallest width of 1600dp. The only thing I can think of is if you were running on a chromebook. – Ben P. Sep 24 '19 at 19:20
  • but then, with your approach I have exactly the same problem that with the approach I tryed in the question. Or not? And does exists a equivalence list between resolutions and dp? If not, how can I name the folders to a correct correspondence between screen size and text size? this is very hard, must be a way to do this easily – NullPointerException Sep 24 '19 at 20:14
  • `dp` is the only unit that (roughly) corresponds with real-world size. There are 160dp per inch. – Ben P. Sep 24 '19 at 20:19
  • so, how can I name the folders and ajust the text sizes to a correct correspondence between screen size and text size to get text sizes which occupies the same portion of the screen for a phrase in all the devices? – NullPointerException Sep 24 '19 at 20:27