androidandroid-studioandroid-layoutnavigationbarandroid-navigation-bar

Android Navigation bar position in landscape orientation


In landscape orientation I need to know if the navigation bar is at the right or left side of the screen so that I can apply paddings to a LinearLayout using WindowInsets to prevent the LinearLayout from overlapping the navigation bar.


Solution

  • I ended up using OrientationEventListener which returns the orientation of the device in form of degrees,the value in orientation depends on how the device is tilted,it ranges from 0-360.You might want to Toast the values in real-time to know which degree would be reverse landscape and which would be landscape,it's hacky but gets the job done.In portrait it ranges from 0-50,in landscape it ranges from 55-120,in reverse landscape it's >250 but can be as low as 200 if orientation is rapidly changed.An orientation of -1 means that the device is lying flat on a surface.In reverse landscape the navigation bar is at the right while in landscape it's at the left.

    orientationListener = new OrientationEventListener(this,
                    SensorManager.SENSOR_DELAY_NORMAL) {
    
                @Override
                public void onOrientationChanged(int orientation) {
                    if(orientation>250){//reverse landscape
                         isReverse=true;
                         }
                    else {
                        isReverse=false;
                    }
                }
            };
    
            if (orientationListener.canDetectOrientation()) {
    
                orientationListener.enable();
            } else {
                orientationListener.disable();
            }