Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Android Navigation Bar orientation

Tags:

Is there a way to know where this Navigation Bar will be displayed for landscape :

  • on Nexus7 it is at the bottom
  • on a nexus 5 it is on the right.

Thanks

enter image description here

like image 642
ebtokyo Avatar asked Jan 11 '14 00:01

ebtokyo


People also ask

How can I change navigation bar orientation in android?

Android already has a rotate button in the quick settings menu which lets you set the screen's orientation to portrait, landscape, or auto-rotate.

How do I show navigation bar on Android?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.


2 Answers

Based in part on Pauland's answer (in turn based on the implementation of PhoneWindowManager), here is what I am using at the moment:

  public static boolean isSystemBarOnBottom(Context ctxt) {     Resources res=ctxt.getResources();     Configuration cfg=res.getConfiguration();     DisplayMetrics dm=res.getDisplayMetrics();     boolean canMove=(dm.widthPixels != dm.heightPixels &&         cfg.smallestScreenWidthDp < 600);      return(!canMove || dm.widthPixels < dm.heightPixels);   } 

This works on a Nexus 7 2012 and a Nexus 4, each running Android 5.1.

On devices that have a permanent MENU key, there is no system bar. Depending upon your use case, you may need to check for this case:

ViewConfiguration.get(ctxt).hasPermanentMenuKey() 

(where ctxt is some Context)

Personally, I am using this to try to have a sliding panel be on the opposite axis from the system bar, as bezel swipes on the side with the system bar are a bit difficult to trigger. I would not use this, or any other algorithm (like the ones that depend upon getDecorView()), for anything critical.

like image 57
CommonsWare Avatar answered Oct 03 '22 21:10

CommonsWare


By using the properties of the decor view in combination with the current DisplayMetrics you can find out on which side the navigation bar is positioned.

// 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; 
like image 23
jankovd Avatar answered Oct 03 '22 21:10

jankovd