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.
Related
I read about dp and pixel and calculate dpi and pixel . but i am confused. I want to know if I want to create UI for my app, is better what resolution I select for beginning? I want to use of Photoshop. on design section in Android studio, there are e few devices with their resolutions,for example (480 * 800 hdpi(nexus one)) and so on . but which one is better for first create on photoshop?
I suggest using px for design
Mobile: 360 x 640
Tablet 7inch: 600 x 960
Tablet 9inch: 1024 x 768
References
https://i.stack.imgur.com/rrNoM.png
https://i.stack.imgur.com/ueFa0.png
https://i.stack.imgur.com/CAt1u.png
Use Density-independent pixel (dp) units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
The android documentation says :
A virtual pixel unit that you should use when defining UI layout, to
express layout dimensions or position in a density-independent way.
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.
You can read it here
For correct icon size you should read answer this
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.
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.
My understanding of the density independent pixel, is that it will render as the same size regardless of the screen dpi and dimensions. I created an image button of with and height of 50dip. On my medium dpi Samsung Galaxy Tab, this button measures about 10mm x 10mm (measured with a ruler. On my large, hdpi Acer Iconia Tab, the button measures about 7mm x 7mm. I would have expected the two buttons to have the same actual size. Am I doing something wrong, or is my understanding incorrect?
Both previous answers are correct. The problem lies in that not only changes the size in inces, but also the number of total pixels.
For a solution, you will need to create different xml layout files in res/layout-normal and res/layout-large, to adjust for screen resolution changes. Still, given that these folders group ranges of devices, you will have some variation in size.
Alternatively, if you strictly need a specific fixed size for a View, set up its width and height as "10mm"
public static final int layout_width Since: API Level 1
Specifies the basic width of the view. This is a required attribute for any view inside of a containing layout manager. Its value may be a dimension (such as "12dip") for a constant width or one of the special constants.
May be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).
http://developer.android.com/reference/android/R.attr.html#layout_width
For a list of screen sizes, resolutions and dpi values, take a look at: http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density
To calculate the real dpi value, check here: http://en.wikipedia.org/wiki/Pixel_density#Calculation_of_monitor_PPI
You've got it the wrong way round, the Galaxy Tab is HDPI and the Acer Tab is MDPI.
50dips is 50 pixels on an MDPI device or 75 pixels on an HDPI device, which should translate to roughly the same physical size.
However things are slightly different due to the much larger screen size on 3.0 tablets. In these cases often using the HDPI assets gives a better size.
from: http://developer.android.com/guide/practices/screens_support.html (emphasis added)
Density-independent pixel (dp)
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.
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.
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.