Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 815
DR Haus Avatar asked Oct 26 '15 20:10

DR Haus


2 Answers

A good approach is Method One from this answer.

To achieve a completely transparent status bar, you have to use statusBarColor, which is only available on API 21 and above. windowTranslucentStatus is available on API 19 and above, but it adds a tinted background for the status bar. However, setting windowTranslucentStatus does achieve one thing that changing statusBarColor to transparent does not: it sets the SYSTEM_UI_FLAG_LAYOUT_STABLE and SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flags. The easiest way to get the same effect is to manually set these flags, which effectively disables the insets imposed by the Android layout system and leaves you to fend for yourself.

You call this line in your onCreate method:

getWindow().getDecorView().setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

Be sure to also set the transparency in /res/values-v21/styles.xml:

<item name="android:statusBarColor">@android:color/transparent</item>

Or set the transparency programmatically:

getWindow().setStatusBarColor(Color.TRANSPARENT);

The good side to this approach is that the same layouts and designs can also be used on API 19 by trading out the transparent status bar for the tinted translucent status bar.

<item name="android:windowTranslucentStatus">true</item>

I'm guessing you've tried this or something similar. If that's not working, make sure your root element is a FrameLayout.

like image 71
Ben Kane Avatar answered Nov 11 '22 13:11

Ben Kane


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)
like image 24
Zain Avatar answered Nov 11 '22 11:11

Zain