- Size of android notification bar and title bar?
- 8 Answers 8
- Height of statusbar?
- 13 Answers 13
- How do I get the height and width of the Android Navigation Bar programmatically?
- 19 Answers 19
- New answer in 2021 comes to the rescue
- Height of status bar in Android [duplicate]
- 23 Answers 23
- Current method:
- (old Method) to get the Height of the status bar on the onCreate() method of your Activity, use this method:
Size of android notification bar and title bar?
Is there a way to obtain the size of the notification bar and title bar in android? At the moment I obtain the display width and height with:
After that I want to subtract the sizes of the bars so that I can stretch a video without losing aspect ratio. Currently I hide the bars because I can’t see a better way.
8 Answers 8
Maybe this is a helpful approach: Referring to the Icon Design Guidelines there are only three different heights for the status (notification) bar depending on the screen density:
- 24px for LDPI
- 32px for MDPI
- 48px for HDPI
So if you retrieve the screen density of the device using densityDpi of DisplayMetrics you know which value to subtract
so it could look something like that:
*As for as I know, this works perfectly.
Martin’s answer specified the wrong height (at least as of SDKv11). As per Icon Design Guidelines, the status bar icons have a height of 25dp, not 32dp. 25dp translates into these density-specific heights:
- 19px for LDPI
- 25px for MDPI
- 38px for HDPI
- 50px for XHDPI
An easy way to observe these sizes is to use the hierarchyviewer against an emulator or device with a normal density. Simply look at the value of getHeight() for the title bar’s FrameLayout. The status bar is a bit trickier because it’s part of the DecorView and not a view in itself, so get its height indirectly by looking at mTop for the title bar’s FrameLayout, which is positioned immediately below the status bar. Per my observations the status bar and title bar each match the 25dp height of the status bar icons (I have not seen a declaration of this fact in the android ref as of SDKv11).
Источник
Height of statusbar?
Is there a way to get the height of the statusbar + titlebar? Checking the dev forum shows the same question but no solution (that I could find).
I know we can get it after the initial layout pass, but I’m looking to get it in onCreate() of my activity.
13 Answers 13
Get the Height of the status bar on the onCreate() method of your Activity, use this method:
For those, like me, who want to use it in your XML layouts:
Don’t be confused by that Android Studio (2.2 Preview 3 — at the moment of writing) doesn’t support this notion. The runtime does. I took it from the Google’s source code.
Although this is an old question, I found that the answer didn’t work in onCreate() :
I found this code from here which does work in the onCreate() method
I hope this helps to anyone that runs into this issue.
The supported way of getting status bar height is to use WindowInsets class starting from API 21+:
or WindowInsetsCompat for support libraries:
You can also override the onApplyWindowInsets method inside the view:
For further details, I’d recommend checking Chris Banes talk — Becoming a master window fitter (slides available here).
You could also take the dimension of the status bar found in the dimens.xml file of android using the way that this blog post describes.
This can get the height of the statusbar without the need to wait for drawing.
Quoting the code from the blog post:
You need to put this method in a ContextWrapper class.
I would suggest next code:
1) Device 1 brand: samsung, device: maguro, board: tuna, cpu_abi: armeabi-v7a, display: ICL53F.M420KREL08, manufacturer: samsung, model: Galaxy Nexus, ver.release: 4.0.2, ver.sdk: 14;
Screen resolution: 1280 x 720.There are no hardware buttons on this device.
Device 1 has title bar at the top of the screen and status bar with software buttons at the bottom of the screen.
2) Device 2 device: bravo, board: bravo, cpu_abi: armeabi-v7a, display: FRG83G, manufacturer: HTC, model: HTC Desire, ver.release: 2.2.2, ver.sdk: 8,
Screen resolution: 800 x 400. This device has hardware buttons.
Device 2 has title bar at the top of the screen and hasn’t status bar at all.
Two solutions were suggested above:
(it is incorrect for device 1 — it gives 0 instead of 72)
(it is incorrect for device 2 — it gives 0 instead of 38)
gives correct results in both cases.
Update
3) One more example (third device): brand: LGE, device: v909, cpu_abi: armeabi-v7a, display: HMJ37, model: LG-V909, ver.release: 3.1, ver.sdk: 12
Device 3 has horizontal orientation, hasn’t title bar at the top of the screen and has status bar at the bottom of the screen.
It’s correct for devices 2 and 3. I am not sure about device 1. User sent me screenshot of device 1. There is a status bar with software button there. But expression «display.getHeight() — v.getBottom()» gives 0.
Attach a runnable to one of your views in your onCreate method, and place the above code in there. This will cause the application to calculate the status bar + titlescreen height when they are attached to the screen.
Take a look at the code below:
If this still doesn’t do the trick, try invoking the view’s postDelayed method instead of post and adding a millisecond value as the second argument.
As of API 23 there is a better solution to getting the status bar height. API 23 adds a WindowInsets feature, so you can use this code to get the size of the system insets, in this case at the top.
Note that getRootWindowInsets() will return null until after the View has been attached to a Window so it can’t be used directly in onCreate() but you can add a listener for the window attach and do it there — here I am adding the status bar inset to the size of my toolbar, which I hide and show, along with the status bar. When it’s shown, I want the status bar over the top of it so I add the status bar height to the toolbar’s top padding.
I think better way to calculate that is to get height of fullscreen minus our main layout
And you can put it in onGlobalLayout() . This works on tablets too, I tried it on Theme.NoTitleBar, but it must always works.
Maybe you can even enhance it and use it onCreate() by changing mainView.getHeight() to mainView.getMeasuredHeight() .
The solution posted by Jorgesys is very good, but it doesn’t work inside onCreate() method. I guess it’s because statusbar and titlebar are created after onCreate().
The solution is easy — you should put code inside runnable and execute it after onCreate() by using root.post((Runnable action) );
So the whole solution:
Ok. Final answer. One that does not have side-effects, relies on documented behavior, supports Q, cutouts, devices with different status-bar size depending on orientation, &c.
The callback occurs after onStart(), before first layout, and occasionally thereafter.
This may seem unrelated but most of the time, the reason that people look for the status bar height is to offset their own views so they are not placed under it.
By setting fitsSystemWindows on the view you want to «push down» to give space to the status bar, it will be done automatically and according to the size of the status bar on each device. Padding will be added to the view that has this property set to true .
Keep in mind that padding will only be added to the first view in the hierarchy with fitSystemWindows set to true
This applies to cases where the status bar is translucent for example. Make sure that you set a Theme to the activity that doesn’t have fitSystemWindows set, otherwise the padding will be added to the activity instead (because it’s first in the hierarchy).
Источник
How do I get the height and width of the Android Navigation Bar programmatically?
The black navigation bar on the bottom of the screen is not easily removable in Android. It has been part of Android since 3.0 as a replacement for hardware buttons. Here is a picture:
How can I get the size of the width and the height of this UI element in pixels?
19 Answers 19
I get navigation bar size by comparing app-usable screen size with real screen size. I assume that navigation bar is present when app-usable screen size is smaller than real screen size. Then I calculate navigation bar size. This method works with API 14 and up.
UPDATE
For a solution that takes into account display cutouts please check John’s answer.
The NavigationBar height varies for some devices, but as well for some orientations. First you have to check if the device has a navbar, then if the device is a tablet or a not-tablet (phone) and finally you have to look at the orientation of the device in order to get the correct height.
Actually the navigation bar on tablets (at least Nexus 7) has different size in portrait and landscape so this function should look like this:
I think better answer is here because it allows you to get even cutout height too.
Take your root view, and add setOnApplyWindowInsetsListener (or you can override onApplyWindowInsets from it), and take insets from it.
In my camera activity, i add padding equal to the systemBars.bottom to my bottom layout. And finally, it fix cutout issue.
with appcompat it is like this
without appcompat, this:
I hope this helps you
This is my code to add paddingRight and paddingBottom to a View to dodge the Navigation Bar. I combined some of the answers here and made a special clause for landscape orientation together with isInMultiWindowMode. The key is to read navigation_bar_height, but also check config_showNavigationBar to make sure we should actually use the height.
None of the previous solutions worked for me. As of Android 7.0 you have to take Multi Window Mode into consideration. This breaks the implementations comparing display.realSize with display.size since realSize gives you the dimensions of the whole screen (both split windows) and size only gives you the dimensions of your App window. Setting padding to this difference will leave your whole view being padding.
New answer in 2021 comes to the rescue
insipred from Egis’s answer:
where the extension getter is
- emulators with navigation bars
- pixel 3a (api 30)
- pixel 2 (api 28)
- pixel 3 (api 25)
- pixel 2 (api 21)
- Xiaomi Poco f2 pro with & without navigation bar(full display)
The solution proposed by Egidijus and works perfectly for Build.VERSION.SDK_INT >= 17
But I got «NoSuchMethodException» during execution of the following statement with Build.VERSION.SDK_INT
I resolved this issue for all devices(including Nexus 5, Samsung Galaxy Nexus 6 edge+, Samsung S10, Samsung Note II etc.). I think this will help you to handle device dependant issues.
Here I am adding two types of codes,
Java Code(for Native Android):
And C# code(for Xamarin Forms/Android)
How to get the height of the navigation bar and status bar. This code works for me on some Huawei devices and Samsung devices. Egis’s solution above is good, however, it is still incorrect on some devices. So, I improved it.
This is code to get the height of status bar
This method always returns the height of navigation bar even when the navigation bar is hidden.
NOTE: on Samsung A70, this method returns the height of the status bar + height of the navigation bar. On other devices (Huawei), it only returns the height of the Navigation bar and returns 0 when the navigation bar is hidden.
This is code to get height of navigation bar and status bar
I’ve done this, it works on every device I tested, and even on emulators:
The height of the bottom Navigation bar is 48dp (in both portrait and landscape mode) and is 42dp when the bar is placed vertically.
Combining the answer from @egis and others — this works well on a variety of devices, tested on Pixel EMU, Samsung S6, Sony Z3, Nexus 4. This code uses the display dimensions to test for availability of nav bar and then uses the actual system nav bar size if present.
Tested code for getting height of navigation bar (in pixels):
Tested code for getting height of status bar (in pixels):
Converting pixels to dp:
Here is how I solved this. I made a hideable bottom bar which needed padding depending on if there was a navigation bar or not (capacitive, on-screen or just pre lollipop).
View
Utils.java
In my case where I wanted to have something like this:
I had to follow the same thing as suggested by @Mdlc but probably slightly simpler (targeting only >= 21):
It works on landscape too:
Edit The above solution does not work correctly in multi-window mode where the usable rectangle is not smaller just due to the navigation bar but also because of custom window size. One thing that I noticed is that in multi-window the navigation bar is not hovering over the app so even with no changes to DecorView padding we have the correct behaviour:
Источник
Height of status bar in Android [duplicate]
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)
23 Answers 23
this question was answered before. Height of statusbar?
Update::
Current method:
ok, the height of the status bar depends on the screen size, for example in a device with 240 X 320 screen size the status bar height is 20px, for 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
so i recommend to use this script to get the status bar height
(old Method) to get the Height of the status bar on the onCreate() method of your Activity, use this method:
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 an Activity 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. an Activity ).
Hardcoding the size or using reflection to get the value of status_bar_height is considered bad practice. Chris Banes talked about this in at the Droidcon New York. The recommended way of getting the status bar size is via the OnApplyWindowInsetsListener:
This was added in API 20 and is also backported via ViewAppCompat.
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
I’ve merged some solutions together:
According to Material Guidance; height of status bar is 24 dp.
If you want get status bar height in pixels you can use below method:
which can be called from activity with:
The default height used to be 25dp. With Android Marshmallow (API 23) the height was reduced to 24dp.
Update: Please be aware that since the age of notches and punch-whole-cameras began, using a static height for the status bar no longer works. Please use window insets instead!
this also work with the refrence link
Official height is 24dp , as is stated officially by Google on Android Design webpage.
I have the same problem of having to get the status bar height in an onCreate. This works for me.
Inside the onCreate:
Yes when i try it with View it provides the result of 25px. Here is the whole code :
320×480 — 25px
480×800+ — 38px
Kotlin version that combines two best solutions
- Takes status_bar_height value if present
- If status_bar_height is not present, calculates the status bar height from Window decor
it didn’t work for me in the onCreate method for the activity, but did when I put it in an onClickListener and gave me a measurement of 25
the height of the status bar is 24dp in android 6.0
you can find the answer in the source code: frameworks\base\core\res\res\values\dimens.xml
To solve this, I used a combination approach. This is necessary as on tablets the system bar already subtracts it’s pixels when display.getHeight() is called. So I first check if a system bar is present, and then Ben Claytons approach, which works fine on phones.
Thanks to @Niklas +1 this is the correct way to do it.
Please excuse me if the code isn’t 100% correct, converted it from Xamarin C# but this is the just of it. Works with Notches, etc.
Toggled Fullscreen Solution:
This solution may look like a workaround, but it actually accounts for whether your app is fullscreen (aka hiding the status bar) or not:
This way, if your app is currently fullscreen, barheight will equal 0.
Personally I had to use this to correct absolute TouchEvent coordinates to account for the status bar as so:
And that will get the absolute y-coordinate whether the app be fullscreen or not.
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.
On Android 4.1 and higher, you can set your application’s content to appear behind the status bar, so that the content doesn’t resize as the status bar hides and shows. To do this, use SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN. You may also need to use SYSTEM_UI_FLAG_LAYOUT_STABLE to help your app maintain a stable layout.
When you use this approach, it becomes your responsibility to ensure that critical parts of your app’s UI (for example, the built-in controls in a Maps application) don’t end up getting covered by system bars. This could make your app unusable. In most cases you can handle this by adding the android:fitsSystemWindows attribute to your XML layout file, set to true. This adjusts the padding of the parent ViewGroup to leave space for the system windows. This is sufficient for most applications.
In some cases, however, you may need to modify the default padding to get the desired layout for your app. To directly manipulate how your content lays out relative to the system bars (which occupy a space known as the window’s «content insets»), override fitSystemWindows(Rect insets). The fitSystemWindows() method is called by the view hierarchy when the content insets for a window have changed, to allow the window to adjust its content accordingly. By overriding this method you can handle the insets (and hence your app’s layout) however you want.
Источник