Change status bar color with AppCompat ActionBarActivity

164,766

Solution 1

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>

Solution 2

There are various ways of changing the status bar color.

1) Using the styles.xml. You can use the android:statusBarColor attribute to do this the easy but static way.

Note: You can also use this attribute with the Material theme.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>
</resources>

2) You can get it done dynamically using the setStatusBarColor(int) method in the Window class. But remember that this method is only available for API 21 or higher. So be sure to check that, or your app will surely crash in lower devices.

Here is a working example of this method.

if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(getResources().getColor(R.color.primaryDark));
}

where primaryDark is the 700 tint of the primary color I am using in my app. You can define this color in the colors.xml file.

Do give it a try and let me know if you have any questions. Hope it helps.

Solution 3

I don't think the status bar color has been implemented in AppCompat yet. These are the attributes which are available:

    <!-- ============= -->
    <!-- Color palette -->
    <!-- ============= -->

    <!-- The primary branding color for the app. By default, this is the color applied to the
         action bar background. -->
    <attr name="colorPrimary" format="color" />

    <!-- Dark variant of the primary branding color. By default, this is the color applied to
         the status bar (via statusBarColor) and navigation bar (via navigationBarColor). -->
    <attr name="colorPrimaryDark" format="color" />

    <!-- Bright complement to the primary branding color. By default, this is the color applied
         to framework controls (via colorControlActivated). -->
    <attr name="colorAccent" format="color" />

    <!-- The color applied to framework controls in their normal state. -->
    <attr name="colorControlNormal" format="color" />

    <!-- The color applied to framework controls in their activated (ex. checked) state. -->
    <attr name="colorControlActivated" format="color" />

    <!-- The color applied to framework control highlights (ex. ripples, list selectors). -->
    <attr name="colorControlHighlight" format="color" />

    <!-- The color applied to framework buttons in their normal state. -->
    <attr name="colorButtonNormal" format="color" />

    <!-- The color applied to framework switch thumbs in their normal state. -->
    <attr name="colorSwitchThumbNormal" format="color" />

(From \sdk\extras\android\support\v7\appcompat\res\values\attrs.xml)

Solution 4

Just paste this function in your Utils class where you keep your all other common functions.

fun Activity.changeStatusBarColor(color: Int, isLight: Boolean) {
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    window.statusBarColor = color

    WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightStatusBars = isLight
}
 

and use it from anywhere like this:

changeStatusBarColor(
        ContextCompat.getColor(
            context,
            R.color.black
        ), false
    )

Note that here I also have managed the dark and light status bar colors separately to manage out icon and text colors of status bar.

Solution 5

Try this, I used this and it works very good with v21.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimaryDark">@color/blue</item>
</style>
Share:
164,766

Related videos on Youtube

tyczj
Author by

tyczj

Updated on December 24, 2021

Comments

  • tyczj
    tyczj over 2 years

    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?

  • tyczj
    tyczj over 9 years
    ah ok I wasnt using getWindow()
  • 124697
    124697 over 9 years
    this would crash when code LOLLIPOP is not found in old androids. its best to use >=21
  • matiash
    matiash over 9 years
    @code578841441 Actually, that should not happen. Constants are inlined when compiling.
  • dbm
    dbm over 9 years
    @code578841441: That is because you're compiling with an older SDK. You should always strive for compiling with the latest Android SDK, even if you have older API version constraints (i.e. minSdkVersion and/or targetSdkVersion attributes on the <uses-sdk ...> element).
  • Philipp E.
    Philipp E. over 9 years
    I also had to call getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_S‌​YSTEM_BAR_BACKGROUND‌​S); to make it work
  • bkurzius
    bkurzius about 9 years
    it looks like window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCEN‌​T_STATUS); is not needed - but this one worked for me
  • TheIT
    TheIT about 9 years
    It's possible that it will never be implemented in AppCompat if the older OS versions do not provide any ability to modify the status bar.
  • Soheil Setayeshi
    Soheil Setayeshi about 9 years
    <attr name="colorPrimaryDark" format="color" /> <!-- Dark variant of the primary branding color. By default, this is the color applied to the status bar (via statusBarColor) and navigation bar (via navigationBarColor). -->
  • SpaceMonkey
    SpaceMonkey over 8 years
    How do you do that for the whole app and not just the activity?
  • matiash
    matiash over 8 years
    @Spacemonkey By setting android:statusBarColor in your theme.
  • SpaceMonkey
    SpaceMonkey over 8 years
    @matiash I meant programmatically
  • matiash
    matiash over 8 years
    @Spacemonkey Oh... there is no way that I know of. You need to call setStatusBarColor() in every activity's onCreate() (or in a base class for all activities, of course).
  • Andrew
    Andrew over 8 years
    Any ideas why the programmatic version would work but the style version doesn't?
  • BMacedo
    BMacedo over 7 years
    In my case the activity's style had the flah translucent_status set, so without the window.clearFlags command it didn't work. So thank you for this!
  • htafoya
    htafoya almost 7 years
    As a note the color int should be retrieved as getColor(res_id) , if the color is taken from your colors xml
  • Bruno Costa
    Bruno Costa almost 7 years
    the getWindow() does not exist in the page context. How are you using?
  • fanjavaid
    fanjavaid over 6 years
    Oh wow! This should be accepted answer, add clearFlags fix my issue
  • Daniel Beltrami
    Daniel Beltrami over 5 years
    I want change to white, but icons stay white also, so they desapear
  • Arbaz.in
    Arbaz.in over 2 years
    this will change color but not in my desire color what is the issue?