2

I don't understand how available height qualifier works. I'm using :

val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(displayMetrics)
    val height = displayMetrics.heightPixels
    val width = displayMetrics.widthPixels
    Toast.makeText(this, "$height", Toast.LENGTH_LONG).show()

to show the available screen height of Nexus One and get 800h. I don't have a bottom navigation bar or action bar on screen. I add a second dimension with height 800 and set my resources but it's not working.

user4157124
  • 2,809
  • 13
  • 27
  • 42
Pako1
  • 247
  • 2
  • 12

2 Answers2

1

The Nexus One has 800 physical pixels in the vertical dimension, but it also has a pixel density of approximately 252 ppi (specs). That means that it has approximately 507 density-independent "pixels" (dp) in the vertical dimension, and this is the number that the available height qualifier uses.

Try -h500dp and see if you get what you expect.

(Note also that the available height qualifier uses the current orientation of the device, so if your Nexus One is in landscape orientation, you'll have much less available height.)

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Thanks a lot for the help!! So basically if i want to figure out what height the phone has i will have to calculate the dp from the physical pixels and the ppi ? – Pako1 Sep 27 '19 at 18:33
  • Yes, assuming that you're working with `dp` units (as with the available height qualifier). The formula is `px / (ppi / 160)`. – Ben P. Sep 27 '19 at 18:34
  • Great!! And one last question, lets say I support now h550 and i add h650 and there comes a device that has a height bigger than 650 then it will take the dimensions from the 650? In what case will it take from the default one? Thanks a lot for clarifying this for me !! – Pako1 Sep 27 '19 at 18:50
  • If you have `values/` + `values-h550dp/` + `values-h650dp/`, then: (1) anything 650dp or greater will use the 650 directory, (2) anything 550-649 will use the 550 directory, and (3) anything 549 or less will use the default directory. – Ben P. Sep 27 '19 at 19:02
0

I believe there are around 4-5 ways to get the screen measurements on a device.

  • getMetrics()
  • getRealMetrics()
  • getDecorView()
  • getWindowVisibleDisplayFrame()

They all provide slightly different results, so it depends on your use case. Try the last method, but it would be good to read into each of them to understand how they are different.

zuko
  • 707
  • 5
  • 12