How to convert PX into DP in Layout - android

Designer has given me a PSD file, He has created it for 1080X1920 resolution
In PSD he defines that the header height should be 52px, Slider height should be 350px and so on..
In layout when i write layout_height=350dp its taking a lot of height and covering more than half of screen.
I am using default layout to accomplish this task.
Kindly guide me how to define Height in layout in DP with respect to PX

Looks like there's a lack of designer-developer flow. The designs you were given are not mobile friendly (Android neither iOS). I recommend you to take a look to a couple of tools for a nice workflow.
Sketch
Zeplin
And please read this article you both:
Designer's guide to DPI
In Android you need to work in dps and you need the designs to be given in dps. In case you are given the designs in pixels you have to have the agreement with the designer that 1px is equals to 1dp, in other words, the designer is working on MDPI basis where 1dp is equals to 1dp.
So the easiest way to go is your designer needs to work in a screen that is 360x640 pixels instead of 1080x1920. Now you can forget about translating pixels to dps and the other way around because 1px would be 1dp for you. For your designer as well will be easier because he needs to go from guidelines to the design tool, and he doesn't need to translate dps into pixels anymore. Everytime he reads 48 dps in the guidelines (which is everywhere) he knows he has to read it as 48 pixels in his 360x640 canvas in the design tool.
Notes
360x640 is a common screen size in dps (Galaxy S5, Nexus 5 and many others) and because of that is taken as a reference now a days. Please realize your designer can not give you the exact sizes for every component in every layout size for every device. So things like the toolbar height (usually 56dp) is easy to translate to every device, but the toolbar width that you'll size in his designs (360 if you follow my recommendation) is completely useless for you, because that depends on the real device. The Galaxy S5 and the Nexus 5 are 360, but the Nexus 4 is 384, and the nexus 6, 6P and 5X is around 413.... and actually there's much more!!
Designing just for one device or one screen size is too simplistic and problematic. Be aware you have to develop responsive layouts ready for a multiwindow.

Related

UI design implementation for multiple screens

I am in android development for last 6 months, recently got a design from a client. I implemented the design and he said this is not exactly look like what the designer has done. Then again he gave me the design with following specifications
my question is - is this right to add the spacing as he has given will this be proper in all devices else how can I explain him what he says is wrong
Please correct me am I wrong or he is wrong
As other users told you, px is not a relative dimension. As an example, we suppose a mobile phone with 5x10 cm screen dimensions, the model has hdpi resolution (480x854 px). For this device, a horizontal margin of 100px takes up more than 20% of the screen width. But, we also have another phone with the same screen dimensions (5x10 cm), but this one is more expensive and it has higher resolution (xxhdpi -> 1080x1920 px). For this case, a horizontal margin of 100 px will not take up more than 10% of the screen width. Therefore, it is needed a relative dimension as dp.
A possible way to deal with your designer could be decide a device to do the designs (a common one). This is a useful link with some devices and their configurations: https://material.io/tools/devices/ . I could recommend you to design using a 720x1080 px (xhdpi) one as MotoG or Nexus 4. For this case, you only need to divide by 2 the px value that he provides you to get each dp value.
You should take in account 3 Things:
Px - dp proportions:
Read this answer : https://stackoverflow.com/a/2025541/5884956 (actually the whole post is useful)
Percentage doesn't work for you:
What you improve by using dp instead of px is that for screens with same
aspect ratio and different pixel density, the layout looks exactly the same (dp-density independent pixel). It means everything inside the screen will look with the same proportions (buttons, imageviews, margins... etc).
Then, you would imagine all android phones have the same aspect ratio (16:9 for example). Which means that for every 16 dp of high, they have 9 dp of width , or in other words if you divide the height of the phone between the width of the phone will give you the same solution as 16/8.
But this is way far from the reality. The most common one is 16:9 but some phones like "Samsung Galaxy S8" have 18,5:9 and there are a lot more aspect ratios: https://material.io/tools/devices/. If you work with %, the time you see your design in a different aspect ratio, your items will look stretched. And this is not the desired.
Size does matter
By this time you should be convinced that dp is the best solution, if not, someone pease improve my answer. Buuuuuut, as the screen size vary, the number of dp also does. So for a phone of bigger dimensions (even for same aspect ratio) we have more dp. Then you should work knowing that some parts are going to be stretched (it should be the spaces between items and margins).
Ideally (Android Studio should do all this work for you) working with same aspect ratio , % of dp should be the mesure and when changing the aspect ratio
this changes must be reflected in spaces between views. But this is a hard work.
TL;DR
Select a standard device that designers are going to use to design (use a common one).
Tell them the design is not going to look exactly the same for every device (unless you design a different layout for each).
They need to decide which parts of the design are going to be the
stretched (stretch spaces and margins please, not buttons or images).
Let them work with px, they may feel more comfortable (meanwhile you must convert it to dp and work with dp). Also if you choose an 'mdpi' device dp = px.
Look at the layouts that allows you work better with this kind of the
designs ( I feel comfortable with ConstraintLayouts, but by working you will notice that this doesn't fit for everything ), but this is up to you.
Extra Information
https://developer.android.com/training/multiscreen/screensizes

Android UI Design and Layout Adjustment with DP unit

I'm working for build the UI of my team's App.
In the beginning, my partner designs UI using Sketch 3.
According to the Android API guide for development Supporting Multiple Screens we known that 1dp = 1px in mdpi.
So we plan to design a basic standard UI in mdpi density. Other density would be automatically fit the position or length if we use dp as our length unit.
The basic UI resolution for designing canvas we use in Sketch is 360 x 640. We've read some articles, some articles said the mdpi resolution for designing standard is 360 x 640, and others said is 480 x 320.
It's really confused. We finally choose 360 x 640, because 480 x 320 is 4:3 aspect ratio not so many Android devices use it.
After we've done our UI, I'm trying to apply it to our app. But I've met some problems with positioning element. For example, our splash page.
Here is our splash page, I can hold Option/Alt to get the distance between edge to out element in Sketch 3. Because the UI sample file was made in mdpi resolution, I can use the length directly.(1px = 1dp in mdpi)
Splash Sample Image
CoolLogo is a ImageView. the whole layout is an RelativeLayout.
I'm using android:layout_centerHorizontal="true" to make ImageView in the center of Layout.
and android:layout_marginTop="268dp" to make it right position according to our UI sample file.
Here is whole layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/LaunchPageLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/coollogo"
android:layout_marginTop="268dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Preview on the Nexus 5(5", xxdpi, 1080 x 1920) looks fine. But the ImageView position seems to be incorrect a little bit.
When switch to smaller device such as Nexus One(3.7", hdpi, 480 x 800), the position is totally wrong. it should be in more upside position.
Nexus One Preview Sample Image in Android Studio
I'm confused. How can we get the right position values when we put our element into layout? Doesn't it means we just only to design a UI in mdpi it should fit all density? but if that is ture, why the layout on small devices is wrong? because our sample UI is design in mdpi resolution. I'm totally muddled.
might somebody give me some advices or some best practice to bulid UI?
Thank you.
According to guideline,
Google is not recommend to use absolutely pixel unit to design UI.
We use the method below to design our application in the end.
Share to everybody who started to design Android UI.
Basically, we need an mdpi size canvas, the canvas size is 320px * 480px
We use this resolution as our standard resolution,
including measuring length and size.
And if you ever heard about the design of iOS UI (or macOS).
You'll know that iOS often use 320px * 480px as standard x1 resolution.
When export resources, there will be x1 for standard resolution and x2 for Retina resolution.
Because the Retina resolution is two times of standard resolution.
Just like that, if we use standard resolution, Android will help us to fit on machines with different resolutions.
so we got:
mdpi => x1 (the standard)
hdpi => x1.5
xhdpi => x2
xxhdpi => x3
Theoretically, if we follow standard resolution, we can build layout for any other resolution.
By the way, We can set different dimen values as well.
it will help the layout to be more accurate in visually.
Maybe the best way to build one layout to fit all kind of resolution currently is to use ConstraintLayout.
but this kind of approach sometimes doesn't make sense.
Anyway, designing UI for phone and tablet (or even phablet) always be a good user experience.

android layout at different screen sizes

trying to get my head around screen sizes so my app looks the same on different devices.
I have two devices a galaxy s3 and a galaxy tab 8. both have the same resolution screen 720 x 1280 but the s3 is 4.7 inches while the tab is 8 inches
When I do a linear layout I set padding to 130 at the top so that the first textview is 130 pixels from the top so the image on the background is not obscured.
This is fine on the s3 but when I load it on the tab the padding needs changing to 190 pixels for it to look the same.
I cant get my head round if the height of the screen resolution is the same (1280) on both devices why do i need larger padding on the screen
is there a way of working out how to make the textview appear on the same location on both devices
Any help appreciated
Mark
Yes there is a way.
First off all you should use DP or SP unit instead of PX. The DP unit have in consideration the size of the screen and the screen resolution. You can see more in here:
What is the difference between "px", "dp", "dip" and "sp" on Android?
Also you can create a folder in res and add different sizes for different devices.
Example:
You already have the folder 'values' with the file 'dimens.xml' in there you can add margins and sizes variables.
If you create the folder 'values-large' and copie the file 'dimens.xml', you can change the sizes of the variables and maintain the name.
In devices 'large' it will load different values from the rest of the devices.
You can see all documentation in here: http://developer.android.com/guide/practices/screens_support.html
Hope it helps you.
You need to add 2 different layouts for 2 different screen sizes. In fact you can provide multiple screen support limited only by your considerations. You can customize layout according to the screen sizes. Here is the official documentation to help you get going.
First of all, you need to use dp instead of px to do these kind of stuff. The reason the result is wrong is that the number of dots per inch on the s3 is much higher than that of the galaxy tab 8. You should really read this link to understand more.
[http://www.captechconsulting.com/blog/steven-byle/understanding-density-independence-android][1]

Android - dp units on layouts -look like bad idea?

Android developers site,and most of you,Push everybody to use dp units on their layouts,
I can understand this approach if you using different layout for different densities,but when my requires is one layout.xml ,and one drawable folder,it is make no sense to me to use it,
because it is NOT preserve proportion.
It seems weird to me that there is no simple way to preserve proportion for all devices.
Just help you to understand my point.
1.make any layout with view inside it
2.set the sizes on dp as Android advise you to do
3.and then check it on the eclipse graphical view of your XML.
It looks way way different between different size devices.
Button with width of 15 dp look like 10 times bigger in 640*480 devices then Tablets.
So what's the point?
I understand all the math of conversion between dp and pixel with density,
but i looking for simple way to preserver the proportion on my layout,without define others for others densities.
The only way that i find to help is using android:weightsum,
But it can not use for margins and other settings.
Any idea?
You can always use your values folder and implements the exact dp size for each of different device densities, so you wont create different layout on different density.
Tablet and handheld devices will never have the same sizes that is where the different layout folders are created to let the developers design on different devices.
I would recommend a different layout design on tablet and handheld devices which most apps applies.
Think of dp units as referring to actual size (like cm and inches). So something that is 250dp will look (for example) about 1 inch on a phone, but also roughly 1 inch on a tablet. No matter the size and resolution of the device, it will always be about 1 inch.
Why is it done this way? Well basically it is the Android creative vision that you won't simply scale your app from a handheld screen to a tablet sized screen. This is why they don't allow you to specify heights and widths as % of screen size. The one small exception is layout_weight in LinearLayout.
You may agree or disagree with their vision, but that's the reality.

Android ImageButton relative width

I'd like to ask about one of the hardest thing for me (as a programmer) in android. As I make software mostly without designer (to be honest - totally without a designer) I can't figure out the following:
When I draw a png I use px (not dp) as image width.
I drew a button image with the following sizes:
hdpi: 318x45
mdpi: 212x30
(6 to 4 ratio)
and I would like my button be 80% width of Galaxy s2 which is
Screen Size in Pixels: 480 Pixel x 800 Pixel Screen Size in dp: 352dp
x 587dp
But also I would like to have the same width (80%) for hdpi 10' tablet. Is it possible or I'm missing something?
And is it possible to make 80% but not more than XXX pixel width?
You just need to provide different size images for different screen sizes and densities. When you think about it there's only a few different formats you need to worry about. You will then just create an "80%" size image for each of those formats, place them in the seperate drawable folders, and use them accordingly.
The android docs on supporting multiplce screen size and providing alternative resources are actually pretty comprehensive on how to do this.
Otherwise I'm sorry you don't have a designer. My tip - make your icons originally in Adobe Illustrator if you have it that way you can easily scale them to whatever size you need. As vector images they won't lose quality so you can change the size all you want. When you're ready, just export for web and devices at the size break points and you should be fine.
You have to use the android:layout_weight attribute.
Details outlined in a similar question here

Categories

Resources