Edited Question:
Mobile Resolution:
I would like to design different screen dpi like following resolutions.
320x480,
48
App launcher icon size in pixels for different resolution
Mobile Resolution
Use following folders if you wish to have tablet-specific layouts:
layout-large-mdpi (1024x600)
layout-large-tvdpi (800x1280)
layout-large-xhdpi (1200x1920)
layout-xlarge-mdpi (1280x800)
layout-xlarge-xhdpi (2560x1600)
Mobile
res/drawable (default)
res/drawable-ldpi/ (240x320 and nearer resolution)
res/drawable-mdpi/ (320x480 and nearer resolution)
res/drawable-hdpi/ (480x800, 540x960 and nearer resolution)
res/drawable-xhdpi/ (720x1280 - Samsung S3, Micromax Canvas HD etc)
res/drawable-xxhdpi/ (1080x1920 - Samsung S4, HTC one, Nexus 5, etc)
res/drawable-xxxhdpi/ (1440X2560 - Nexus 6,Samsung S6edge).
Tablet Resolution:
NOTE: Always try to use SP whenever you deal with
textSize
, liketextsize=12sp
Use predefined textAppearance
:
It will set text size automatically as per device density.
<TextView android:textAppearance="?android:attr/textAppearanceSmall"/>
<TextView android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView android:textAppearance="?android:attr/textAppearanceLarge" />
Sample usage:
<TextView
style="@android:style/TextAppearance.Small"
android:text="Sample Text - Small" />
<TextView
style="@android:style/TextAppearance.Medium"
android:text="Sample Text - Medium" />
<TextView
style="@android:style/TextAppearance.Large"
android:text="Sample Text - Large" />
Use dimension.xml
for each device:
From Google IO Pdf, we see structure below:
Mobile:
res/values/dimens.xml(default)
res/values-ldpi/dimens.xml (240x320 and nearer resolution)
res/values-mdpi/dimens.xml (320x480 and nearer resolution)
res/values-hdpi/dimens.xml (480x800, 540x960 and nearer resolution)
res/values-xhdpi/dimens.xml (720x1280 - Samsung S3, Micromax Canvas HD, etc)
res/values-xxhdpi/dimens.xml (1080x1920 - Samsung S4, HTC one, etc)
res/values-xxxhdpi/dimens.xml (1440X2560 - Nexus 6,Samsung S6edge).
Tablet:
For tablet you can use more specific folder like values-xlarge
, values-large
.
res/values-large/dimens.xml (480x800)
res/values-large-mdpi/dimens.xml (600x1024)
or
res/values-sw600dp/dimens.xml (600x1024)
res/values-sw720dp/dimens.xml (800x1280)
res/values-xlarge-xhdpi/dimens.xml (2560x1600 - Nexus 10")
res/values-large-xhdpi/dimens.xml (1200x1920 - Nexus 7"(latest))
For further information:
Refer to Supporting Multiple Screens.
See Page# 77 of Google IO Pdf for Design device density. In that, you will find the way to handle
dimens.xml
for different different devices.- Getting Your Apps Ready for Nexus 6 and Nexus 9.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple:
px = dp * (dpi / 160)
. For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
I think this is a good answer:
Text size and different android screen sizes
But here how you can do it with screen resolution :
You can create "values" resource directory for each resolution like
values-wWIDTHp-hHEIGHTdp (you can also use values-wWIDTHp or values-hHEIGHTdp)
for example: 320*480 will be values-w320p-h480dp
In each dir (including default values dir) create a file named "dimens.xml" with content:
for exmaple (the value related to the resolution):
<dimen name="def_font_size">10sp</dimen>
Now you can use the "@dimen/def_font_size" or create a style in the default values directory.
Add this to the "styles.xml":
<style name="FontSize">
<item name="android:textSize">@dimen/def_font_size</item>
</style>
Basically you need to create a Text Style something like this:
<style name="CodeFont">
<item name="android:textSize">30sp</item>
</style>
read more about it here http://developer.android.com/guide/topics/ui/themes.html
And using the android support for different screens guide to create the different sizes you want for different screens in the right res folders as described in: http://developer.android.com/guide/practices/screens_support.html
Side note: I don't really understand in what situation would you want to do that, using SP units for font sizes will scale the fonts to look more or less the same size on different phones.
I have created a function which convert size of dp to the size according to screen size and it is working well for me. Any one having problem with text size according to screen should give this a try.
public float convertFromDp(int input) {
final float scale = getResources().getDisplayMetrics().density;
return ((input - 0.5f) / scale);
}
simple give the value given by it to the text view size programically as below
tvTextView1.setTextSize(convertFromDp(24));
For nexus 6 device support , create drawable-560dpi folder and put all images in it.
First you create the different values folders for different screens.and put the size according to the screens
in res->values->dimens.xml
file and call the simple font size using "@dimen/text_size"
.
res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-xlarge/dimens.xml
//for small
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">15sp</dimen>
</resources>
//for normal
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">20sp</dimen>
</resources>
//for large
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">30sp</dimen>
</resources>
//for xlarge
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">40sp</dimen>
</resources>
and retrieve the font size in TextView
as given below.
<TextView
android:id="@+id/lblHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/text_size"
android:textStyle="bold"
android:typeface="serif" />