Transparent status bar (with visible navigation bar)
Asked Answered
D

9

19

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

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

Dymphia answered 20/7, 2021 at 8:22 Comment(2)
Still looking for solution ? @DymphiaUdometer
Check this link - #68507284Riyal
G
30

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

Gloat answered 22/7, 2021 at 23:43 Comment(5)
This doesn't actually make the status bar transparent, it makes it translucent and will still have a shadow to itSalpingitis
@Salpingitis I updated the answer with the fix of this .. Thanks for notifying me.Gloat
what about toolbar?Kolb
@Kolb Which toolbar? the OP was asking about the status & navigation bars. You can hide the ActionBar from the activity's style.Gloat
@Gloat anyway here's the correct and simple answer https://mcmap.net/q/110795/-transparent-status-bar-with-visible-navigation-bar transparent toolbar can also be added and it will be under status barKolb
A
9

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

Adventure answered 22/7, 2021 at 16:0 Comment(0)
C
2

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
    }
Crenshaw answered 12/2, 2022 at 14:27 Comment(0)
C
2

i do this for my app inside onCreate

with(window) {
        setFlags(
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
    }

it works for me, and i hope it works for you too.

Carport answered 1/3, 2022 at 7:20 Comment(0)
O
1

I have achieved similar result by following approach.

    <style name="Theme.WebImageDownloader" parent="Theme.MaterialComponents.DayNight.NoActionBar">
     
    // make status bar icons colour grey
    <item name="android:windowLightStatusBar">true</item>
    // override the notch area
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    // make status bar transparent
    <item name="android:windowTranslucentStatus">true</item>
    // make navigation are transparent
    <item name="android:windowTranslucentNavigation">true</item>
   </style>

And set above theme in the manifest

 android:theme="@style/Theme.WebImageDownloader">

Further more, i have placed my tool bar in the activity as follows,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/holo_blue_dark"
android:layout_height="match_parent">

// other views 

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="32dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ToolBarStyle" />

</RelativeLayout>

There 'ToolBarStyle', you can have any colour for background , text etc.

Ocampo answered 20/7, 2021 at 10:12 Comment(1)
Thanks for the answer but isn't what I asked forDymphia
A
0
private fun setStatusBar() {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
        )
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT
        window.navigationBarColor = Color.TRANSPARENT
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }
Al answered 23/7, 2021 at 11:31 Comment(0)
M
0

After days of searching, and literally have read all stackoverflow related posts. I hit to this below settings which worked for me. As I just played around and suddenly ran into it, so I have no idea why it worked.

SDK : minSdkVersion 28 targetSdkVersion 30

So, most of the suggestions were using deprecated APIs and most likely like this :

        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">false</item>

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

        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

And what i got with those settings: 1

Then it came to that grey status bar which i saw many people, same as me, wanted to get rid of it.

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

I disabled that translucentStatus, and handle the display behind status bar using code :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_MyApp);
        getWindow().setDecorFitsSystemWindows(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);

I got this result after that : 2

Lastly, we change the status bar text color:

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

And it works: 3

:)

Msg answered 20/8, 2021 at 4:22 Comment(0)
L
0

For who used a whole day to find how to make the status bar completely transparent.

Firstly, remove the code like those: as my personal opinion, those setting is easily to mess up everything.

This one will make the spinner inside scrollview not working.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

 View.setSystemUiVisibility() 

 final View decorView = window.getDecorView();
 decorView.setSystemUiVisibility();

After you clean up the code that you tried before.

Add this into the activity OnCreated method.

WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

And add this into your theme file, base on you want to make navigation bar or status bar transparent

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

Add this if you need to setup the status bar with darker text color

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

Ref: https://developer.android.com/training/gestures/edge-to-edge

Lille answered 29/7, 2022 at 9:15 Comment(0)
S
-1

I have tried all answers with different android versions.. and what worked with me:

  • Don't set the android:windowTranslucentStatus as true, it will make a transparent status bar but with dark shadow.
// no need to add this.. it is false in default
<item name="android:windowTranslucentStatus">false</item>
  • call this method in your activity to make the status bar transparent even for android 11 and 12
public void transparentStatusBar() {
        Window window = getWindow();
        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
            );
        }
        window.setStatusBarColor(Color.TRANSPARENT);
}
  • If you found the navigation bar overlays the bottom views.. Just add padding at the bottom and avoid this :)
  • To set the status bar tint black
<item name="android:windowLightStatusBar" tools:targetApi="M">true</item>
Sinner answered 31/3, 2022 at 17:26 Comment(2)
"Just add padding at the bottom" isn't an easy task. Some device have a bottom notch, some others don;t even have a soft navigation bar and still return a height for it. This whole thing is a total mess.Refutation
Just add padding at the bottom and avoid this, that is just really badDerange

© 2022 - 2024 — McMap. All rights reserved.