Dynamically change status bar color without losing transparency
Asked Answered
D

3

5

I know I can use colorPrimary to determine the color of Toolbar, and colorPrimaryDark to determine the color of Status bar.

I'm using the following theme

<!-- Base application theme. -->
<style name="Theme.Noteplus.Base.Brown" parent="Theme.AppCompat.Light.DarkActionBar">

    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimaryLight</item>
    <item name="colorPrimaryDark">#ff0000</item>

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

One of the interesting attribute is, when I slide out the navigation menu, the status bar becomes transparent automatically.


During run-time, sometime I would like to change the color of status bar.

setTitle("Recycler bin");
toolbar.setBackgroundColor(Color.BLUE);
getWindow().setStatusBarColor(Color.parseColor("#5694FF"));

It will looks as follow

Unfortunately, calling setStatusBarColor, will also loss the transparency attribute of status bar, when we slide out the navigation menu.

May I know, how to change status bar color during run-time, without lossing its transparency attribute? For my case, after I changing the status bar to blue during run-time, when I slide out navigation drawer, I wish to see status bar transparency attribute being retained.


Update

I had tried

private void setStatusBarColor(int color) {
    if (Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(color);
    }
}

It doesn't help to provide transparency attribute when the navigation drawer slides out.

Denning answered 6/5, 2018 at 15:35 Comment(6)
did you try this ? ActionBar bar = getActionBar(); bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#004D40")));Stickseed
We have already did getWindow().setStatusBarColor(Color.parseColor("#5694FF"));, as stated in my question.Denning
YEah this is setBackgroundDrawable not setColorStickseed
We don't have ActionBar in our app. We use Toolbar. getActionbar() will return null.Denning
my bad i confused the actionbar with statusbar, i added an answer, tho i dont have AndroidStudio to test right now but i think it will helpStickseed
please check https://mcmap.net/q/169606/-transparent-status-bar-not-working-with-windowtranslucentnavigation-quot-false-quotIntersperse
D
6

You are using a DrawerLayout. That means, that instead of using Window#setStatusBarColor(int) you should be using one of DrawerLayout#setStatusBarBackground() overloads.

The equivalent of your code is following:


    ColorDrawable colorDrawable = new ColorDrawable(0xFF5694FF);
    drawerLayout.setStatusBarBackground(colorDrawable);

I've applied minor changes to the template app that can be created with Android Studio wizard:

enter image description here

Deservedly answered 8/5, 2018 at 19:21 Comment(0)
S
0

I guess your problem with transparency is not about the method you are using but its rather about the color itself! You are using this getWindow().setStatusBarColor(Color.parseColor("#5694FF"));

Try to use RGBA instead of RGB so make it transparent, should be something like this:

getWindow().setStatusBarColor(Color.parseColor("#5694FF80"));

Stickseed answered 8/5, 2018 at 15:58 Comment(3)
That wouldn't help. i.imgur.com/9lmRAkg.png (We use #7f757575). We can use color without transparency in colorPrimaryDark (Which is #ff0000), yet retain status bar transparency attribute.Denning
tell me if this will change anything: Window win = getWindow(); win.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); and use it after chaning the colorStickseed
in case this worked, (removed the status bar at all) then you can refer to this to find the correct flags you need to make it transparent or as android like to call to it TRANSLUCENT : developer.android.com/reference/android/view/…Stickseed
Q
0
private void changeStatusBarColor(String color){
    if (Build.VERSION.SDK_INT >= 21) {
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.parseColor(color));
    }
}

If you check here in stack overflow everyone before that line of code set the transparency of the status bar to solid with

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
Quartering answered 8/5, 2018 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.