Is there any way to get the size of navigation bar in android in xml file similar to this?
android:paddingTop="?android:attr/actionBarSize"
where actionBarSize is navigation bar size?
The height of the bottom Navigation bar is 48dp (in both portrait and landscape mode) and is 42dp when the bar is placed vertically. That's even better.
The Navigation bar is the menu that appears on the bottom of your screen - it's the foundation of navigating your phone. However, it isn't set in stone; you can customize the layout and button order, or even make it disappear entirely and use gestures to navigate your phone instead.
After looking in android source it looks like there is dimens for navigation bar height :
@android:dimen/navigation_bar_height
There are other dimens linked to nav bar, examples from android values/dimens.xml :
<!-- Height of the bottom navigation / system bar. -->
<dimen name="navigation_bar_height">48dp</dimen>
<!-- Height of the bottom navigation bar in portrait; often the same as @dimen/navigation_bar_height -->
<dimen name="navigation_bar_height_landscape">48dp</dimen>
<!-- Width of the navigation bar when it is placed vertically on the screen -->
<dimen name="navigation_bar_width">42dp</dimen>
Try this code:
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
As suggested in many of similar questions, for example this, this, this, and this, simply getting navigation bar height may not be enough. We need to consider whether 1. navigation bar exists, 2. is it on the bottom, or right or left, 3. is app open in multi-window mode.
There is a simple one line solution
android:fitsSystemWindows="true"
or programatically
findViewById(R.id.your_root_view).setFitsSystemWindows(true);
you may also get root view by
findViewById(android.R.id.content).getRootView();
or
getWindow().getDecorView().findViewById(android.R.id.content)
For more details on getting root-view refer - https://stackoverflow.com/a/4488149/9640177
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With