What's the height of the status bar in Android? Is it always the same?
From my measurements it seems that it's 25dp, but I'm not sure if it has the same height on all platforms.
(I want to know this to properly implement a fade transition from an activity that doesn't have status bar to one that does)
Out of all the code samples I've used to get the height of the status bar, the only one that actually appears to work in the
onCreate
method of anActivity
is this:Apparently the actual height of the status bar is kept as an Android resource. The above code can be added to a
ContextWrapper
class (e.g. anActivity
).Found at http://mrtn.me/blog/2012/03/17/get-the-height-of-the-status-bar-in-android/
This issue recently became relevant for me because of the notch in my Pixel 3XL. I really liked android developer's solution, but I wanted to be able to get the status bar height at will, since it was specifically necessary for a full screen animation that I needed to play. The function below enabled a reliable query:
And then in the activity class:
You will of course want to make pass a weak reference of the activity to the static Helper class method parameter, but for the sake of brevity I refrained in this example. The
blurBitmap
anderrorHudFrameLayout
are omitted for the same reason, since they don't directly pertain to obtaining the height of the status bar.I've merged some solutions together:
another alternative:
EDIT: Alternative to runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126
If you know exactly the size VS height
like
for example in a device with 320 X 480 screen size the status bar height is 25px, for a device with 480 x 800 the status bar height must be 38px
then you can just get the width of your view / the screen size you can just use an if else statement to get the height of status bar
On MDPI devices, the status bar is 25px. We can use this as the base and multiply it by the density (rounded up) to get the status bar height on any device:
For reference: ldpi=.75, mdpi=1, hdpi=1.5, xhdpi=2
The reason why the top answer does not work for some people is because you cannot get the dimensions of a view until it is ready to render. Use an
OnGlobalLayoutListener
to get said dimensions when you actually can:This is the most reliable method.