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.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

3 Answers3

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

azizbekian
  • 60,783
  • 13
  • 169
  • 249
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"));

Yamen Nassif
  • 2,416
  • 2
  • 24
  • 48
  • That wouldn't help. https://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. – Cheok Yan Cheng May 08 '18 at 16:04
  • 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 color – Yamen Nassif May 08 '18 at 16:08
  • 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 : https://developer.android.com/reference/android/view/WindowManager.LayoutParams – Yamen Nassif May 08 '18 at 16:14
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)
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157