Search code examples
androidandroid-6.0-marshmallowandroid-statusbarandroid-navigation-bar

Android Draw behind Status bar but not behind the navigation bar


I'm looking into a way where the status bar is completely transparent, not translucent and where the Navigation Bar is left untouched.

Closest I can get is to use the flag

WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS

but this draws behind the Navigation Bar as well.

The background is what is making this tricky, it is a subtle gradient, and when I set the status bar color to the start of the gradient, it looks almost right, but has a subtle line across the top of the screen.

Is there a good way to fit the activity to the window, only at the top but if there is a navigation bar at the bottom, leave it alone?


Solution

  • Disclaimer: This is complementary answer as the accepted answer is valid without deprecation > till API level 30 because the system visibility flags are deprecated as of API level 30.

    setDecorFitsSystemWindows() implements edge-to-edge to your app (i.e. the app will be expanding its content to extend across the entire screen to cover both the system status & navigation bars)

    And this can be implemented in the activity with:

    WindowCompat.setDecorFitsSystemWindows(window, false)
    

    Now the remaining part is to avoid the overlapping with the system navigation bar:

    As per documentation:

    You can address overlaps by reacting to insets, which specify which parts of the screen intersect with system UI such as the navigation bar or the status bar. Intersecting can mean simply being displayed above the content, but it can also inform your app about system gestures, too.

    So, We need to handle the insets for API level 30+ to avoid the overlapping between the app and the bottom navigation bar:

    /*
    *  Making the Navigation system bar not overlapping with the activity
    */
    if (Build.VERSION.SDK_INT >= 30) {
    
        // Root ViewGroup of my activity
        val root = findViewById<ConstraintLayout>(R.id.root)
    
        ViewCompat.setOnApplyWindowInsetsListener(root) { view, windowInsets ->
    
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
    
            // Apply the insets as a margin to the view. Here the system is setting
            // only the bottom, left, and right dimensions, but apply whichever insets are
            // appropriate to your layout. You can also update the view padding
            // if that's more appropriate.
    
            view.layoutParams =  (view.layoutParams as FrameLayout.LayoutParams).apply {
                leftMargin = insets.left
                bottomMargin = insets.bottom
                rightMargin = insets.right
            }
    
            // Return CONSUMED if you don't want want the window insets to keep being
            // passed down to descendant views.
            WindowInsetsCompat.CONSUMED
        }
    
    }
    

    Check documentation for more info.

    Extra cent:

    • If you're going to use <item name="android:windowTranslucentStatus">true</item> for lower API levels, then you have to override the themes/styles in API-30; i.e. to have a res\values-v30\themes.xml with the default style (of course without windowTranslucentStatus)