Different font sizes for different screen sizes

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

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...)


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.


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.


Need Your Help

What SqlDbType maps to varBinary(max)?

c# asp.net sql-server types

What SqlDbType maps to varBinary(max)? SqlDbType.VarBinary says that it is limited to 8K. SQL Server documentation says that varbinary(max) can store aprrox. 2GB. But SqlDbType.VarBinary says that ...

android listview item style?

android listview styles

before i posted this thread , I've googled ( how to style listview items ) i cannot find a good example to showing how to style listview item ( normal , touch , long click etc ) background colors a...