Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent status bar (with visible navigation bar)

I know this question has been asked many times, but all of the answers either not working, or use deprecated code:

  • Android Completely transparent Status Bar?
  • Transparent status bar - before Android 4.4 (KitKat)
  • Lollipop : draw behind statusBar with its color set to transparent

I want to achieve the same effect as latest google maps app:

enter image description here

  • Fully transparent status bar (Only status bar. not navigation bar!)
  • Non deprecated solution

WindowCompat.setDecorFitsSystemWindows(window, false) partially working as it also hides the navigation bar also

like image 960
dor506 Avatar asked Jul 20 '21 08:07

dor506


People also ask

How can set action bar transparent in Android?

In my manifest file I put android:theme="@style/AppTheme. ActionBar. Transparent" in the activity tag as shown and it would not work for me. Then I moved it inside the application tag (line 11 for me) and it worked.

What is hide status and navigation bar?

Hiding the status bar (and optionally, the navigation bar) lets the content use more of the display space, thereby providing a more immersive user experience.

How to make status and navigation bar transparent in Xcode?

Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “TransparentViews” Step 2 − Embed the View Controller in Navigation Controller. Add Image View and shown and add image. Step 3 − Run the application without adding any piece of code for making status and navigation bar transparent.

How to create transparent StatusBar and Actionbar in Android?

How to create transparent statusbar and ActionBar in Android? This example demonstrates how to create a transparent statusbar and ActionBar in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How to make the status bar fully visible in light mode?

SYSTEM_UI_FLAG_LIGHT_STATUS_BAR makes sure that the status bar icons are drawn in such a way so that they are fully visible in light mode. Cool..so, now our status bar looks pretty good on most of the API levels. But but.. what's up with the Floating Action Button.

What is the difference between status bar and navigation bar?

The status bar is given a window where the system draws things like time, battery, notification icons etc. The navigation bar also has a different window where it draws the back button, the home button etc. All these windows on a single screen are managed by WindowManager. What are these different flags you can apply to a window?


Video Answer


3 Answers

Step 1: To make the status bar transparent: add the below into the style themes.xml or sytles.xml:

<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>

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

Step 2: Then in activity to make the status bar overlaps with the activity:

The used window flags are deprecated as of API level 30, so they can be used till API level 29:

if (Build.VERSION.SDK_INT in 21..29) { 
    window.statusBarColor = Color.TRANSPARENT
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    window.decorView.systemUiVisibility =
        SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or SYSTEM_UI_FLAG_LAYOUT_STABLE

} else if (Build.VERSION.SDK_INT >= 30) {
    window.statusBarColor = Color.TRANSPARENT
    // Making status bar overlaps with the activity
    WindowCompat.setDecorFitsSystemWindows(window, false)
}

UPDATE For API-30

This doesn't actually make the status bar transparent, it makes it translucent and will still have a shadow to it

This is right on API-30, and reason because setting <item name="android:windowTranslucentStatus">true</item>.

Actually the <item name="android:windowTranslucentStatus">true</item> is only required on API level 19. If your app is greater than that, you can dismiss it at all.

Anyways, the way to fix this is to override the themes.xml/styles.xml in API-30; i.e. to have a res\values-v30\themes.xml; you can just add the main app theme like:

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.TransparentStatusBar" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
     </style>
</resources>

UPDATE 2 For API-30

Just discovered a bug on API 30 that the bottom navigation overlaps with the activity obscuring the bottom part of it, that probably couldn't be discovered by the OP as they are using a map.

To solve this, 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 System bars insets for API level 30+ to avoid your app overlapping with the navigation bar:

This requires the top root ViewGroup of the activity layout, and accordingly the LayoutParams need to be coasted appropriately.

Here I am using a ConstraintLayout as a root layout, and FrameLayout.LayoutParams:

/*
*  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
    }

}

This is tested on 8 devices/emulators on the range of API level 19 to API level 30.

enter image description here

like image 87
Zain Avatar answered Oct 18 '22 19:10

Zain


To make the StatusBar completely transparent,

First, set its color as transparent in the theme or by code as :

//In Theme
<item name="android:statusBarColor">@android:color/transparent</item>
//In Code
window.statusBarColor = android.R.color.transparent

Then, to draw the view behind the StatusBar, use this code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    window.setDecorFitsSystemWindows(false)
} else {
    window.setFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
}

You can also play with the boolean property android:windowLightStatusBar, it sets the StatusBar's text and icon color as black or white, You can do it programmatically as well. You can also set color to your NavigationBar if you don't want to hide it.

Output:

enter image description here

like image 7
Lalit Fauzdar Avatar answered Oct 18 '22 21:10

Lalit Fauzdar


Add this to your theme

<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar" ns1:targetApi="23">true</item>

And use this method

     private fun handleStatusBar() {
        //set fullScreen (draw under statusBar and NavigationBar )
            WindowCompat.setDecorFitsSystemWindows(window, false)

            container.setOnApplyWindowInsetsListener { view, insets ->
            val navigationBarHeight = WindowInsetsCompat.toWindowInsetsCompat(insets)
                .getInsets(WindowInsetsCompat.Type.navigationBars()).bottom

            view.updatePadding(bottom = navigationBarHeight)
            WindowInsetsCompat.CONSUMED.toWindowInsets()
        }

        //make statusBar content dark
        WindowCompat.getInsetsController(window, window.decorView)?.isAppearanceLightStatusBars =
            true

        window.statusBarColor = Color.TRANSPARENT
    }
like image 2
Fayçal Avatar answered Oct 18 '22 19:10

Fayçal