qwertz
qwertz

Reputation: 6286

How to set background color to transparent status bar

Is it possible to change the background color of a transparent status bar programmatically?

I have set my status bar to be transparent using the following code so I can also have my navigation drawer in the status bar without the status bar overlapping the navigation drawer.

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

The status bar takes the background color from

<item name="colorPrimaryDark">@color/primary_dark</item>

Is it possible to change this color programmatically and have the status bar still be transparent?

I tried using

window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(statusBarColor);

but the status bar has a ugly dark scrim over it, which I do not want.

Is there any other way to archieve this programmatically? I need to be able to change the color at runtime. Also having different styles for each color is not an option because the user should be able to select the color.

EDIT: This is what I mean by the status bar overlapping the navigation drawer when changing its color by using setStatusBarColor(int);. I still want the navigation drawer to overlap the status bar.

enter image description here

Upvotes: 2

Views: 2223

Answers (1)

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20910

Use this way.

// Check if the version of Android is Lollipop or higher
if (Build.VERSION.SDK_INT >= 21) {

    // Set the status bar to dark-semi-transparentish
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);


}

UpDate :

Instead of

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

Use the following:

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

UpDate2 :

styles.xml

 <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
 </style>

colors.xml

 <color name="colorPrimary">#0072BA</color>
 <color name="colorPrimaryDark">#004F80</color>
 <color name="colorAccent">#005D96</color>

Output :

without drawer

enter image description here

with drawer

enter image description here

Upvotes: 1

Related Questions