25

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?

fragon
  • 2,921
  • 9
  • 33
  • 72
  • you mean "`ActionBar`" by "nagivation bar"? If you need to get the `ActionBar`'s height, refer to this question http://stackoverflow.com/questions/12301510/how-to-get-the-actionbar-height – Droidman Sep 01 '14 at 10:33
  • 3
    No, I mean navigation bar (the one which is diplayed on devices without hardware buttons). – fragon Sep 01 '14 at 10:58

3 Answers3

30

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>
Gaëtan Maisse
  • 11,587
  • 9
  • 41
  • 45
6

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;
Community
  • 1
  • 1
Max
  • 5,493
  • 4
  • 26
  • 42
1

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

mayank1513
  • 4,971
  • 3
  • 23
  • 70