Search code examples
androidorientationwindowinsets

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


Solution

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