How can I get the height of recent android phones' notch, preferably in pixels?
I have an implementation where an image MUST be at the top in full screen.
With the addition of a notch to recently released phones, this image will be cut out by the notch. My idea is to get the notch height and add a margin above it dynamically to accommodate different notch sizes.
Any help or other approaches would be greatly appreciated.
The easiest way to get the cutout's height in pixels is to override
onAttachedToWindow()
function of your View. View being attached to window is the main requirement torootWindowInsets
being non-null. Note that it's safe to check SDK version, because complete support of cutout has been started since Android Pie.In case that your View isn't CustomView you should add
OnAttachStateChangeListener
:Don't forget to change
LayoutParams(MATCH_PARENT, WRAP_CONTENT)
for your own needs.