androidandroid-layoutandroid-resolution

Assign px value to layout across all Android devices


I am designing a layout as per a set of specification. I basically need to give some space between the layout and the android device. Below image is for reference

enter image description here

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

I am giving px value in xxxhdpi column and getting its respective dp value, which I am placing it in my xml layout.

The primary phones that I am testing on is Samsung S4 and Nexus5

Following are my questions:

  1. According to this site Nexus5 and Samsung S4 belongs to xxhdpi resolution but still I am getting the correct pixel value if I use the xxxhdpi column in the above mentioned AndroidPixel Converter site. Why is this ?
  2. Nexus5 has soft navigation bar where as Samsung S4 has hard button as navigation. How can I detect it and accordingly give values to my layout. The navigation bar, if I am not wrong is of 144px in height, if not present will change the layouts dimension. How can I handle this ?

Lastly, is the method which I am following the correct one? If no, please guide in the correct way.

P.S: I am looking for pixel perfect layouts across all devices. Is it possible ?


Solution

  • You need to talk to your designer and go about things in a different way. You NEVER want to specify a layout in px like that. It won't scale- a px can be any size. Go back to him and get a dp number instead (a dp is basically 1/160th of an inch). Design the whole thing in dp. Then let the phone itself translate it to px.

    If you design in pixels, it will not work. Design in physical dimensions (dp), and let the device translate.

    Also, you can't assume the height of the nav bar. It can, and does differ between devices both in terms of dp and px.