In my application I must use a smaller font for the medium density devices. Is it possible to specify that?
-
you can find answer from here http://stackoverflow.com/questions/16706076/font-size-and-images-for-different-devices/16910589#16910589 – Bhavesh Jethani Jul 16 '14 at 06:48
3 Answers
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...)

- 7,478
- 3
- 37
- 32
-
10Please 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 '11 at 02: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 '11 at 06: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 05 '12 at 07:43
-
It would make more sense to do this using the screen's physical size rather than the DPI no? – Learn OpenGL ES Aug 16 '12 at 02: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 '12 at 13:40
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.
-
3In 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 '11 at 14:03
-
1You can also use "sp" for a unit specification, which will scale it relative to the user's preferred font size. EDIT: Beaten by CodeAttack :) – Kevin Coppock Jan 17 '11 at 14:04
-
1Thanks for the replies! I was aware about the usefulness of using dip. I always use this. But in this case it's not solving my problem. The thing is that the layout on my G2(320x480) must look as it looks on Milestone(480x800); it must be proportional with this one. So using the styles combined with the density - specific values folders gives me the liberty to set the font sizes that I want. – Gratzi Jan 17 '11 at 14:45
-
2No, 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 '11 at 02:53
-
@hackbod Using just dp won't solve my problem. I need the 480 pixels height screen look just like the 800 pixels height one. I mean, the 2 screens must be proportional. – Gratzi Jan 18 '11 at 07:16
-
@Gratzi The G2 and Milestone have different screen ratios so the layout will always look slightly different whatever you do. You can make the layout look good on all devices - current and future - by using dp and sp units. You can't have a custom layout for each device which is what you appear to be trying to do. – Intrications Oct 20 '11 at 08:07
-
1Using DP is not working for me as well. On a 10" tablet, the text displays just fine. But, unless I'm doing something wrong, if I use the same DP for a phone, the text is so big it does not even fit the screen. You need to use DP if you want the text to (roughly?) the same size on different devices (i.e. if put side by side, they are identical). If you want to scale everything down (or up) to fit the screen, DP or SP is not a solution. BTW, we do know every possible screen density out there: ldpi, mdpi, hdpi, xhdpi and tvdpi. It's a good thing Android abstracts it for us. – AngraX May 01 '12 at 21:01
-
3Note 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 02 '12 at 08: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 '12 at 13:38
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.

- 1,826
- 22
- 41