Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android navigation bar size in xml

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?

like image 661
fragon Avatar asked Sep 01 '14 10:09

fragon


People also ask

What is the height of the Android navigation bar?

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.

What is the Android navigation bar?

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.


3 Answers

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>
like image 92
Gaëtan Maisse Avatar answered Oct 27 '22 05:10

Gaëtan Maisse


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;
like image 27
Max Avatar answered Oct 27 '22 06:10

Max


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

like image 1
mayank1513 Avatar answered Oct 27 '22 06:10

mayank1513