Getting the height of Android device independent of pixel densities

Table of contents

TLDR (too long didn’t read)
Why does this matter?

Resources

density multiplier documentation
display metric documentaion

My app on the Google play store

The app

My app’s GitHub code


This content originally appeared on DEV Community and was authored by Tristan Elliott

Table of contents

  1. TLDR (too long didn't read)
  2. Why does this matter?

Resources

My app on the Google play store

My app's GitHub code

TLDR (too long didn't read)

  • When trying to get the height of a Android device, make sure you do it in an pixel density independent way:
  • correct way to get height:
val displayMetrics = Resources.getSystem().displayMetrics
val heightPixels = displayMetrics.heightPixels //exact physical pixel amount(different on certain devices)
val density = displayMetrics.density //density multiplier
val pixelDensityIndependentHeight =heightPixels / density

  • wrong way to get height:
val height = Resources.getSystem().displayMetrics.heightPixels
//as previously mentioned, this height will change depending on the 
// device's pixel density and it can really mess up your UI

Why does this matter?

  • I have created THIS UI FEATURE which relies heavily on my app being able to understand the height of the device and dividing it into 3 sections. If I don't use the height in a pixel density independent way(unfortunately I did this) the UI will look different on different devices. Which obviously is not ideal
  • So to summarize, make sure you are conscious of pixel densities in your UI

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.


This content originally appeared on DEV Community and was authored by Tristan Elliott


Print Share Comment Cite Upload Translate Updates
APA

Tristan Elliott | Sciencx (2024-07-18T22:39:16+00:00) Getting the height of Android device independent of pixel densities. Retrieved from https://www.scien.cx/2024/07/18/getting-the-height-of-android-device-independent-of-pixel-densities/

MLA
" » Getting the height of Android device independent of pixel densities." Tristan Elliott | Sciencx - Thursday July 18, 2024, https://www.scien.cx/2024/07/18/getting-the-height-of-android-device-independent-of-pixel-densities/
HARVARD
Tristan Elliott | Sciencx Thursday July 18, 2024 » Getting the height of Android device independent of pixel densities., viewed ,<https://www.scien.cx/2024/07/18/getting-the-height-of-android-device-independent-of-pixel-densities/>
VANCOUVER
Tristan Elliott | Sciencx - » Getting the height of Android device independent of pixel densities. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/18/getting-the-height-of-android-device-independent-of-pixel-densities/
CHICAGO
" » Getting the height of Android device independent of pixel densities." Tristan Elliott | Sciencx - Accessed . https://www.scien.cx/2024/07/18/getting-the-height-of-android-device-independent-of-pixel-densities/
IEEE
" » Getting the height of Android device independent of pixel densities." Tristan Elliott | Sciencx [Online]. Available: https://www.scien.cx/2024/07/18/getting-the-height-of-android-device-independent-of-pixel-densities/. [Accessed: ]
rf:citation
» Getting the height of Android device independent of pixel densities | Tristan Elliott | Sciencx | https://www.scien.cx/2024/07/18/getting-the-height-of-android-device-independent-of-pixel-densities/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.