4

I simply need to detect if the navigation bar Is located to the right of the screen as seen in image below. Thanks

screenshot with navbar to the right

Zong
  • 6,160
  • 5
  • 32
  • 46
Michael Kern
  • 639
  • 8
  • 15
  • possible duplicate of [Detect Android Navigation Bar orientation](http://stackoverflow.com/questions/21057035/detect-android-navigation-bar-orientation) – NPike May 05 '14 at 18:45
  • Please how to change the current orientation location for the navigation bar? – Benny Mar 22 '17 at 18:40

3 Answers3

2

The following snippet could help:

private boolean isNavigationBarRightOfContent(){
    Rect outRect = new Rect();
    ViewGroup decor = (ViewGroup) mActivity.getWindow().getDecorView();
    decor.getWindowVisibleDisplayFrame(outRect);
    DisplayMetrics dm = getResources().getDisplayMetrics();
    return dm.widthPixels == outRect.bottom;
}
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
1

The Navigation Bar will only be to the right of the screen if it is in landscape mode. So to detect this, use getResources().getConfiguration().orientation like this:

String orientation = getResources().getConfiguration().orientation;
if(orientation.equals("ORIENTATION_LANDSCAPE"){
    // screen in landscape, do what you want to do
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
  • Thank you for the answer but I know this. The thing is that on some devices (most), the NavBar is not to the right but on the bottom. – Michael Kern Feb 08 '14 at 23:11
  • 1
    @ImmaWake Why do you need to detect this (just wondering)? And as to that... I'm not sure how you would detect it. – hichris123 Feb 08 '14 at 23:11
  • well I posted this question> http://stackoverflow.com/questions/21058087/android-disable-overlay-of-actionbar-and-navigationbar-for-flag-translucent a couple weeks ago and got no answers that helped. So my work around was adding padding to the top and bottom of my layout matching statusbar and navbar width – Michael Kern Feb 08 '14 at 23:14
  • This is no longer always true. If the new gesture navigation is being used, the (very thin) navigation bar ALWAYS appears at the bottom of the screen. – Matt Robertson Oct 09 '19 at 21:11
1

Try this:

    // retrieve the position of the DecorView
    Rect visibleFrame = new Rect();
    getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleFrame);

    DisplayMetrics dm = getResources().getDisplayMetrics();
    // check if the DecorView takes the whole screen vertically or horizontally
    boolean isRightOfContent = dm.heightPixels == visibleFrame.bottom;
    boolean isBelowContent   = dm.widthPixels  == visibleFrame.right;
mtwain
  • 101
  • 5