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?
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?
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