Lucid Dev Team
Lucid Dev Team

Reputation: 641

Detect if the Navigation Bar is located right of screen

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

screenshot with navbar to the right

Upvotes: 4

Views: 2850

Answers (4)

Zain
Zain

Reputation: 40878

You can use Insets API and register OnApplyWindowInsetsListener to check the height of the system navigation bar to know the orientation direction:

ViewCompat.setOnApplyWindowInsetsListener(
    window.decorView 
) { _: View, windowInsets: WindowInsetsCompat ->

    val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())

    if (insets.bottom > 0) {
        // Navigation Bar at Bottom

    } else if (insets.right > 0) {
        // Navigation Bar at Right

    } else if (insets.left > 0) {
        // Navigation Bar at Left

    }

    WindowInsetsCompat.CONSUMED

}

Upvotes: 2

mtwain
mtwain

Reputation: 101

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;

Upvotes: 1

Lazy Ninja
Lazy Ninja

Reputation: 22537

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;
}

Upvotes: 2

hichris123
hichris123

Reputation: 10233

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
}

Upvotes: 1

Related Questions