Assign px value to layout on all android devices

I design the layout according to a set of specifications. I basically need to give some space between the layout and the android device. Below pictures for your reference

enter image description here

I am using the following site to convert a given pixel to dp values AndroidPixelConverter

I give the px value in the xxxhdpi column and get the corresponding dp value which I place in my xml layout.

The primary phones I'm testing are Samsung S4 and Nexus5

Below are my questions:

  • According to this site Nexus5 and Samsung S4 belongs to xxhdpi resolution, but still I get the correct pixel value if I use the xxxhdpi column in the above AndroidPixel Converter site. Why is this?
  • The Nexus5 has a soft navigation bar where the Samsung S4 has a hard button for navigation. How can I detect it and give values ​​to my layout accordingly. The navigation bar, if I'm not mistaken, is 144px high, if not, this will resize the layouts. How can I handle this?

Finally, is the method I am following the correct one? If not, please use the correct guidance.

PS: I am looking for perfect pixel layouts on all devices. Is it possible?

+3


source to share


1 answer


You need to talk to your designer and go differently. You NEVER want to specify the layout in px this way. It won't scale - px can be any size. Go back to it and get the dp number instead (dp is basically 1 / 160th inch). Work out everything in dp. Then let the phone translate it to px.

If you are creating pixels it won't work. Design in physical dimensions (dp) and let the device translate.



Also, you cannot accept the height of the navigation bar. It can and does differ between devices in both dp and px terms.

+2


source







All Articles