I got a layout design which states sizes in px which I save in the dimen resource file.
Is there an automatic way to somehow let Android convert the px unit automatically to sp for strings or dp for other sizes, without the need for me to call a method every single time which converts the unit?
If not, how can I define specific sizes in the layout's xml if all the units are in px?
You need to call a function all the time,
or you need to create custom views that extends the views and override the size/textSize function that converts them automatically
class CustomTextView : AppCompatTextView {
override fun setTextSize(size: Float) {
// do the convertion here
super.setTextSize(size)
}
override fun setTextSize(unit: Int, size: Float) {
// or here
super.setTextSize(unit, size)
}
}
and you will probably need to extend every view that has been using dimens if you don't want to write a function all the time.
if you read here you will understand that what you want isn't the best solution.
I would suggest to you to change everything px to dp and then put those on the files
most probably your app will work for more than one density screens so the pixel way is very bad choice
Designing layouts for dp
When designing layouts for the screen, calculate an element’s
measurements in dp:
dp = (width in pixels * 160) / density
For example, a 32 x 32 px icon with a screen density of 320 equals 16
x 16 dp.
Update
unfortunately what you want cannot be happen. You will have to generate different dp values for different density screens. Read also how to support diffent screen densities from the official documentation
Unfortunately, px to dp conversion can not be done by the system automatically for you.
however, you can define several values folders for various screen sizes and define different dimensions(in dp) for each of them.
refer this question for more details:
How to define dimens.xml for every different screen size in android?
Conversion from PX to dp or sp is something you have to do yourself.
and to conclude, you should not be doing this as most of the time values defined in dp and sp suits to most of the screen sizes, so try that first.
Related
Hi Iam developing android Titlebar , so I need to clarify which is use to android text either sp or dip
or
Which is standard and what is the difference between sp and dip? Thanks!
SP is always used for textSize
DP is used for pretty much everything else
See "Supporting Different Densities" for more info.
From the docs:
One common pitfall you must avoid when designing your layouts is using absolute pixels to define distances or sizes. Defining layout dimensions with pixels is a problem because different screens have different pixel densities, so the same number of pixels may correspond to different physical sizes on different devices. Therefore, when specifying dimensions, always use either dp or sp units. A dp is a density-independent pixel that corresponds to the physical size of a pixel at 160 dpi. An sp is the same base unit, but is scaled by the user's preferred text size (it’s a scale-independent pixel), so you should use this measurement unit when defining text size (but never for layout sizes).
sp is used for text size and dp for everything else. These are mainly to achieve the best support for multiple screens with different sizes and density
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 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.
i'm trying to understand if the margin values of the Relativelayout in android are in pixels or dpi ?
thanks.
In xml you specifies measuring units such as android:layout_marginTop="13dp". See documentation about measuring units.
In code you specifies masuring units in pixels (for example margins), but you can read dimensions from resources, where you can specify dimension in dp or sp, etc.
You can use both pixels and dpi. But if you want to make your application for multiple screen support then you have to prefer dpi. Because it will adjust the size according to screen size.
You can choose. Enter either "5px" or "5dp", and it will adapt itself.
I guess you are refering to the values of certain methods. they are in pixels so you better convert the dpi values to pixels using displaymetrics
I am confused about android's multiple screen support. http://developer.android.com/guide/practices/screens_support.html I read this article, but still something is not clear for me.
I used dp instead of px in my layout
I put high, medium, low version's of an image to drawable directories.
I made this changes according to this article. But in some densities, there is still problem although some of them work very well.
The question is what is the exact width and height in dp units for variety of android screen types. if it is changeable, what is the difference between px?
px is changeable, dp is changeable too??? what is the difference??
if changeable, should I change the view's width and height by code on Create function or create seperate layouts for each screen dentisies? Please give a way to understand this...
Thanks in advance..
px are not changeable. dps or dips are.
To calculate how many px your object specified in dps will be use the formula below:
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.
px is a fixed measure. This means that if 100px on a small screen take up 1/2 of the screen, it will take up much less on a large screen. dp = density (independent) pixels, is based on the densitity of the device. So if you specify a width to 50dp on a small screen, it will expand on a large screen. Note that dp is not an insurance of layout compability on all devices, since devices have different aspect ratios. To build a perfect layout, that looks exactly the same on all devices, you must use more techniques. Linearlayout allows you to assign weights. Look into that. http://developerlife.com/tutorials/?p=312