20

In my application I must use a smaller font for the medium density devices. Is it possible to specify that?

1

3 Answers 3

31

You should use styles, then you can have separate folders "values" (default) "values-hdpi" (high density) "values-mdpi" (medium density) and so on and put your style file with correct textSize values in each folder as needed.

Then, when you are in medium density device it will pick the file in "values-mdpi" folder if exists or in "values" if not, and the same for high density etc...

This same principle applies to al "res" subfolders (drawables, values, etc...)

5
  • 10
    Please note, this is wrong. Using different styles just to scale dimensions is NOT RIGHT, and will cause your application to break in the future when it is run on a device with a density you didn't account for. The correct thing to do is use "dp" units.
    – hackbod
    Jan 18, 2011 at 2:54
  • If I understand correctly Gratzi's words "I was aware about the usefulness of using dip. I always use this", what he wanted to do is for example set a textSize of 12dp for mdpi and 14dp for hdpi, so this is the way to go to accomplish this. Also for the screen densities you don't account for there is the "values" folder for defaults...
    – maid450
    Jan 18, 2011 at 6:18
  • @maid450 you said "and put your style file with correct textSize values in each folder as needed." whats the correct size? how to calculate them? because this formula not works very well: sp = pt * ( dpi /72 )
    – CooL i3oY
    Aug 5, 2012 at 7:43
  • It would make more sense to do this using the screen's physical size rather than the DPI no? Aug 16, 2012 at 2:52
  • 12
    @hackbod dip doesn't make anything proportional. It makes that things look the same in all screen resolutions. If you put a device 320x480 and a 480x800 next to each other and compare 100 dip it will have the same physical size, yes. But you still have less space available in the 320x480 screen. So using dip or sp you still have to make things smaller, to fit in the 320x480 screen
    – User
    Nov 22, 2012 at 13:40
29

Specify all your fonts using dips (e.g. 14dp) rather than pixels (e.g. 14px) and you won't need to worry about screen density. Android will scale your fonts (and layout) accordingly.

Edit: Here's comparison of sp/dp from the Android docs:

dp Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, so 160dp is always one inch regardless of the screen density. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. You should use these units when specifying view dimensions in your layout, so the UI properly scales to render at the same actual size on different screens.

sp Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.

9
  • 3
    In case Gratzi is unaware, also specify widths and heights in SP rather than pixels. The answer below about Styles is a good tip about managing look and feel across your app but Dave is spot on about using DP to specify font size.
    – C0deAttack
    Jan 17, 2011 at 14:03
  • 1
    You can also use "sp" for a unit specification, which will scale it relative to the user's preferred font size. EDIT: Beaten by CodeAttack :) Jan 17, 2011 at 14:04
  • 2
    No, using dp units WILL make your size scale proportionally for the density. That is the absolutely correct way to do this. Using styles for this is wrong. It assumes that you know every possible screen density you will encounter, but you don't, so your app will break in the future when it is run on a new density. Just use dp. That is what it is for.
    – hackbod
    Jan 18, 2011 at 2:53
  • 3
    Note you can also name dimensions by specifying them in a file (e.g. values/dimens.xml). That way you can use different DP values for different screen DPIs by creating additional dimension files (such as values-ldpi/dimens.xml and values-xhdpi/dimens.xml). The advantage of this method being you continue to use DP/SP values and yet retain the apparent control PX units give you.
    – Dave
    May 2, 2012 at 8:08
  • 2
    @hackbod dip doesn't make anything proportional. It makes that things look the same in all screen resolutions. If you put a device 320x480 and a 480x800 next to each other and compare 100 dip it will have the same physical size, yes. But you still have less space available in the 320x480 screen. So using dip or sp you still have to make things smaller, to fit in the 320x480 screen.
    – User
    Nov 22, 2012 at 13:38
0

yes, for implementing an universal app which will be working for all resolution. You should configure multiple drawables & corresponding layouts.

eg.

drawables:-

drawable-ldpi drawable-mdpi drawable-hdpi

layouts:-

layout-small layout-medium layout-large

then you can change according to your resolution required for Device. Android supports internal configuration for Density factor of various Screen's resolution. the device can take itself as appropriate drawable & corresponding layout. you dont need to adjust any line of code in your src files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.