In android applications, we maintain dimen.xml for various screen resolutions. Say for example I am using device X as base development device and defining dimen.xml relative to device X. Now if I want to know what will be corresponding dimen.xml for a different density device what procedure/strategy can be followed here?
For example:
If I define margin_10 as 10dp in dimen.xml. what value margin_10 will have for different dimen.xml depending on density type (hdpi, xhdpi, xxhdpi, xxxhdpi)?
Thanks
Since you are defining the size in dp units, you need not worry about giving multiple sizes for other resolutions.
From the developer docs:
A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi.
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.
It's the image resources or bitmaps which are provided for various densities
To generate images, you should start with your raw resource in vector format and generate the images for each density using the following size scale:
xhdpi: 2.0
hdpi: 1.5
mdpi: 1.0 (baseline)
ldpi: 0.75
Hope this helps.
values folder containing dimens.xml file in which whatever screen size you
specified, it is consider as baseline as per google doc means 10dpx1 as per your question..
now you set this size for different devices than procedure is like following.. create folder for different Screen Size
1) values->dimens.xml [default as per base line hdpi] 10dp x 1.0
2) values_sw320dp ->dimens.xml [xhdpi] 10dp x 1.5 result set to dimens.xml file of values_sw320dp folder
3) values_sw480dp -> dimens.xml [for xxhdpi] 10dp x 2.0 result set to dimens.xml file of values_sw480dp folder
4) values_sw640dp -> dimens.xml [for xxxhdpi] 10dp x 2.5 result set to dimens.xml file of values_sw640dp folder as like you can set size for all devices as per google document..
here you just need to create folder and inside folder create dimens.xml file for working with different screen size in android.
Related
I have made this very simple calculator layout. But its different for 480 DPI and 420 DPI on my phone. There is free space on right part of screen in 420DPI and buttons are thinner than 480DPI. I used dp as a units of measurements for buttons and sp for text.
I read Supporting Different Screen Sizes | Android Developers
but couldn't really solve my problem.
I have read that dp unit is independent of DPI of the screen and buttons will scale up or down dynamically. Then how come my buttons get thinner on different DPI even after i used dp as an unit?
I put a file in res folder, the file name is values-w410dp ,because Nexus 5X 1920 x 1080 px (730 x 410 dp) normal 420 dpi.
You can try. It can control layout.
You need to have multiple layout folders in res. Name one of them layout-xxhdpi and put your layout xml there. Devices with density >= 480dpi will load layouts from this folder. Another layout folder should be named layout. Devices with density lower than 480dpi will load layouts from layout. In order to support multiple screens, layouts must have same ID in both folders. You can read more about resource qualifiers here http://developer.android.com/guide/topics/resources/providing-resources.html
I am developing android application that will work on wide range on android mobile phones (not tablets).
Now designer give me designed PSD files and ask me to set the size of elements according to it.
Now in these files for example an element is having height and width of 90px, how many DP should I set it on my android XML layout file?
More I am creating a single layout for ldpi, mdpi, hdpi and xdpi.
I tried http://angrytools.com/android/pixelcalc/ to calculate the same,
but if I choose 90PX, it shows different DP size for ld, md, hd and xd. How can I set DP size in a single layout file that can run on variety of devices?
Please advice,
Regards,
Bhavin.
but if I choose 90PX, it shows different DP size for ld, md, hd and
xd. How can I set DP size in a single layout file that can run on
variety of devices?
=> For your information, DP stands for Density Independent Pixels and that's where the difference between PX and DP comes into the picture.
So if you really want to provide compatibility to all the resolutions/density then you would have to declare dp values in different dimens.xml under different values directory.
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen and 1.5 physical pixel on 240 dpi screen. This can be calculated using px = dp * (dpi / 160).
Ratio for declaring density values is: xhdpi: 2.0, hdpi: 1.5, mdpi: 1.0 (baseline), ldpi: 0.75
Standard practice:
Standard practice is to declare dp values (with same name in all values directory) in different dimens.xml under different values directory and reference the particular dimension value wherever required. Based on the density value, Android system will decide itself to use particular density value (I mean dp value to be taken from particular dimens.xml file).
For example:
values/
dimens.xml
values-land/
dimens.xml
values-xlarge-land
dimens.xml
values-large
dimens.xml
You can convert the dp to pix from following link.
http://labs.rampinteractive.co.uk/android_dp_px_calculator/
(OR)
you can calculate the pixels based on device density.
LDPI - 0.75
MDPI - 1.0
HDPI - 1.5
XHDPI- 2.0
you can keep single layout.xml and create multiple values directory(with different dimension values) for varying devices.
One way is you create single layout.xml and multiple values directory for varying devices.
Other way is DP as Dyou know is Density Independent Pixels, so to use DP calculate the DP value of pixel base on one screen resolution HDPI,XHPDI,LDPI,MDPI etc recommended (HDPI).As you have One psd ask your UI designer for which screen type is that based on that calculate the DP help of angrytools and use That DP in your layout.As Dp is also not fully accurate on various device need some adjustment some time use Value directories at that time.
Thanks.
I am in the process of creating layouts for a new application and I am interested to know how you are usually approach this process.
Obviously it's a very general question, but I think we can get a lot of ideas from different developers' perspectives. As creating perfect layouts in Android, in my opinion, is one of the most difficult tasks in the app development process, I think the answers can be of value.
The assumption is that we want to support all device sizes with the least number of XML files, without compromising the application design quality.
In my case the designer of the app designed it with Galaxy S3 dimensions in mind (720 x 1280px).
Considering the 3:4:6:8 ratio -
What will be the base device you'll do the design for, so one layout file will look good on all normal size devices?
How would you handle the difference in device dimensions, thinking in dp -
Eg.
480 X 800 hdpi = 320 X 533dp
320 X 480 mdpi = 320 X 480dp
720 x 1280 xhdpi = 360 X 640dp
How will you still use the extra dp, so there won't be to many empty spaces, without making the layout not appropriate for 320 X 480 device?
When the designer marks a margin of 20px, how would you interpret it in the xml?
Taking into consideration he designed it on a 720 x 1280px canvas.
Start with some screen size as your base target device for e.g. 4.7 inch (hdpi) and create a single layout for default screens in layout folder and declare dimensions in dimens.xml in values folder.
Next, let's say you want your layouts for a 7" device, create values-sw320dp folder for 7" in portrait orientation and declare separate dimensions in dimens.xml in this folder.
Next, let's say you want your layouts for a 10" device, create values-sw720dp folder for 10" in portrait orientation and declare separate dimensions in dimens.xml in this folder.
PS: For landscape orientation, add -land in the end of folder names.
Next, let's say you have new devices such as xhdpi and xxhdpi devices, you can create values-xhdpi and values-xxhdpi folders.
Since you have defined dimensions in values folder, when you run the App, most of the dimensions will be used from here only. And you can add/update the dimensions in respective folders for screen-size and resolutions on which there is any mismatch.
Also, if you have got separate drawables for various size like mdpi, hdpi, etc you can place them under drawable-mdpi, drawable-hdpi, etc.
The Android system will take care of loading correct resources at run-time.
Note: On Android versions < 3.2, the folder naming was little different, you can refer that here.
Hope this helps.
Android automatically scales layouts to fit the current device. One way to make the solution more elegant would be to create separate folders drawable-ldpi, drawable-mdpi, drawable-hdpi, drawable-xhdpi and drawable-xxhdpi to hold the drawable resources - this is to make sure the same resource does not get used across all the different resolutions where it might not be so clear.
Do not use px (pixels) for measurements in the layout file - use dip (or density independent pixels) which allows to scale according to device pixel density.
The other extreme would be to have different layouts for different resolutions though this is is not advisable.
I want my app to have the ability to run on multiple screen sizes and I was hoping I could store text size values inside the different drawable folders so the text would fit based on screen size. Or is there a better practice for this problem?
EX:
drawable-hdpi << textsize == 50
drawable-mdpi << textsize == 25
drawable-ldpi << textsize == 10
The answer by #wsanville was correct if you want a an identical look and it scales correct, however if for some reason you want completely different sizes. For example, you are using a different layout on the tablet than you do on the phone and you want it to be a header on 1 and a subheader on the other. Then I'd recommend you define different dimen folders.
- values/dimens.xml
<dimen name="textSize">16sp</dimen>
- values-large/dimens.xml
<dimen name="textSize">32sp</dimen>
- values-xlarge/dimens.xml
<dimen name="textSize">32sp</dimen>
The above example would give you a small look on the phone and a large look on the tablets.
The simplest thing do to would be to define your text size in dp (density independent pixels) or sp (scaled pixels).
Doing so, Android will automatically take account for the density by using the following calculations:
ldpi = 0.75x
mdpi = 1x (baseline)
hdpi = 1.5x
xhdpi = 2x
So, suppose you define your text as 12dp. It will end up being 12 pixels on a medium density device, 18 pixels on a high, and so on.
Like alextsc notes, in the case of text sizes, it's best to use sp for your unit, because it will take account for a user preference for font size that was added in ICS. On lower versions of Android, sp is simply equivalent to dp.
In my application I must use a smaller font for the medium density devices. Is it possible to specify that?
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.