Search code examples
androidandroid-5.0-lollipopandroid-toolbar

Change status bar color with AppCompat ActionBarActivity


In one of my Activities, I changed the Toolbar color using Palette. But on 5.0 devices using ActionBarActivity the status bar color is the color of my colorPrimaryDark in my activity theme so I have 2 very different colors and it does not look good.

I realize that in 5.0 you can use Window.setStatusBarColor() but ActionBarActivity does not have this.

so my question is in 5.0 how can I change the status bar color with ActionBarActivity?


Solution

  • I'm not sure I understand the problem.

    I you want to change the status bar color programmatically (and provided the device has Android 5.0) then you can use Window.setStatusBarColor(). It shouldn't make a difference whether the activity is derived from Activity or ActionBarActivity.

    Just try doing:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.BLUE);
    }
    

    Just tested this with ActionBarActivity and it works alright.


    Note: Setting the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag programmatically is not necessary if your values-v21 styles file has it set already, via:

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