Reputation: 641
I simply need to detect if the system navigation bar is located to the right of the screen as seen in image below.
Upvotes: 4
Views: 2850
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
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
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
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