I have a dimen.xml defined in values folder
<dimen name="dimen_button_border_width">2dp</dimen>
<dimen name="version_txt_size">14sp</dimen>
How if I need to add dimen for different values folder like mdpi,hdpi,xxhdpi . What happens to the value of 2dp& 14sp in corresponding folders. Is there any mathematical formula ?
check out what these units are in here
in short: 1dp (density pixel) means 1*density_factor in px, no matter of folder
so 1dp on mdpi device is 1px, on xxhdpi device is 3*1px and so on
you don't need to put same integer in different qualifying folders, it will be calculated same way. but you may when you want e.g. bigger space on low-res devices (different value)
for example - you have thick line separator, very thin, height is 0.5dp. on xxxhdpi device it will be 0.5 x 4 = 2px. but same value for mdpi device will be 0.5 x 1 = 0px (because of int rounding) and your thick separator is gone... in this case you may put in values-mdpi and values-hdpi (1.5 factor) folders different, fixed value in px already instead of dp (in above example it should be 1px overriding 0.5dp -> 0px)
How if I need to add dimen for different values folder like mdpi,hdpi,xxhdpi
You do not need to add dimension resources for different density-based resource sets.
What happens to the value of 2dp& 14sp in corresponding folders. Is there any mathematical formula ?
14sp is the same in all densities. 2dp is the same in all densities. So, the mathematical formula is equality.
Related
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 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 making an Android application for multiple screen device.
I want to know what should be the text size scale ratio for different density device?
example : if my text size is 28sp on mdpi device then what should be on hdpi device?
I made values-ldpi, values-mdpi, values-hdpi etc. and define text size in dimens of each respective values folder, but it did not look similar on all device.
You dont need to know the ratios, and you shouldnt directly use them since the amount of densities keep changin.
If you use it in the resources, using the sp unit will do it work fine in every density, without diferent value folders.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="type1">28sp</dimen>
</resources>
If you do it from code, probably it will look diferent depending on howyou asign the values. You have to send the correct values to the setTextSizeFunction, for example:
setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.type1));
//he documentation method that COMPLEX_UNIT_PX returns a Resource dimension value multiplied by the appropriate metric.
or with hardcoded numbers:
setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);
Use below formula for declaration in dimens.xml
base mdpi= 1X
HDPI= 1.5X
XHDPI=2.0X
XXHDP=2.75X
above formula for relation b/W all Resolution
I'm having trouble understanding the dimen resource attribute.
I have 3 value folders: value-normal, value-large and value-xlarge.
I have defined the titlesize as 20 for normal, 25 for large and 30 for xlarge, this is my normal value file:
<resources>
<dimen name="titleSize">20sp</dimen>
</resources>
But when I try to get this value
Resources r = getResources();
tvTitleSize = r.getDimension(R.dimen.titleSize);
tvTitleSize is equal to 40 which is offcourse to big. Why is the value the double of the original and how can I fix this?
Ps: my device is galaxy nexus.
If you read the docs of Resource.getDimension() method, you find this:
Resource dimension value multiplied by the appropriate metric.
In other words you are getting the actual size in pixels for the device.
The sp is density independent value. The value corresponds to mdpi, but Galaxy Nexus is xhdpi, which is twice as dense as mdpi. That's why you are getting 20 * 2 = 40;
If you are wondering, what is wrong, then the answer is nothing. The size you are getting is correct. The actual text size is different for ldpi/mdpi/hdpi/xhdpi devices.
So for example 16dp means:
12px on ldpi
16px on mdpi
24px on hdpi
32px on xhdpi
The difference between sp and dp is that sp is affected by user preference for font size.
The value you get back is measured in pixels. The 20sp on a high density screen correspond to more pixels than on a low density screen. Android does the conversion for you.
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.