Jovan
Jovan

Reputation: 4794

Android font size on different screens

I want to use different font size for different screen sizes.

I read about this many articles, but I'm not sure about usage. Is correct to use different dimens resource file for different screen dimensions like code below:

res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml

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

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <dimen name="text_size">24sp</dimen>
</resources>
...

I also read that correct way is to use sp for font size, but this doesn't fit font in different screen dimensions as I expect.

If there, what are the disadvantages of using different dimens for every size?

Thanks

Upvotes: 2

Views: 694

Answers (3)

GrkEngineer
GrkEngineer

Reputation: 2132

I do something very similar and it's worked fine for me. Some people put the values in the various layouts such as Fietser suggested, but if all of your layouts end up being the exact same except for the font size, your approach is better. That way you can have a single layout and only modify the font sizes. But sometimes you might have changes in the actual layout xml, so then it's probably a wash between the two approaches.

Upvotes: 0

barwnikk
barwnikk

Reputation: 976

@up Not, it isn't good way.

@topic You can gets width & height of screen (and w&h of View). Next, you can set font, for example 2% of width screen. If you have content 1260x720, 0.02*1260=24,6 px (you can use also (int)24.6 to convert double to int)

Upvotes: 0

Fietser
Fietser

Reputation: 11

The best way is to create different layout resources for each of the screens you wish to support. Place each of the layouts in a separate folder that designates the width of the screen. For example, normal sized layouts go in your res/layout folder, and a layout resource for a 7 inch tablet (600 pixel width) would go in the res/layout-sw600dp folder. Make the resource names identical, but adjust your font sizes accordingly.

Upvotes: 1

Related Questions