Status Bar color for API Level below 21 is not changing

12,337

Solution 1

Status bar coloring is not supported below API level 21. However you can use a few techniques by which you can do it upto API Level 19.

Add this to your build.gradle file:

compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

In your activity before setContentView method call this method:

private void initStatusBar() {
    Window window = getWindow();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setTintColor(ContextCompat.getColor(this, R.color.primaryDark));
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        window.setStatusBarColor(Color.TRANSPARENT);
    }


}

After doing this in your activity_layout.xml file add this attribute to top level layout:

android:fitsSystemWindows="true"

This is how it appears on Lollipop and above:

enter image description here

and this is on kitkat:

enter image description here

Solution 2

because under API 21 ,the change of status bar color is not supported

Share:
12,337
Samarth Kejriwal
Author by

Samarth Kejriwal

Hands on experience in mobile application development. Machine Learning Enthusiast.

Updated on June 11, 2022

Comments

  • Samarth Kejriwal
    Samarth Kejriwal almost 2 years

    The Status Bar color for API level 21 or above is changing according to my requirement but how to change the color for API level below 21.

    Below are the screenshots for both the API's

    API level 21:

    API level 19:

    colors.xml

        <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#3F51B5</color>
        <color name="colorPrimaryDark">#303F9F</color>
        <color name="colorAccent">#FFFFFF</color>
        <item name="b" type="color">#FF33B5E5</item>
    
        <item name="p" type="color">#FFAA66CC</item>
    
        <item name="g" type="color">#FF99CC00</item>
    
        <item name="o" type="color">#FFFFBB33</item>
    </resources>
    

    Style.xml

    <resources>
    
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBarOverlay">false</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>
    
    <style name="AppTheme.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    
    <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
        <item name="tabSelectedTextColor">@color/colorAccent</item>
    </style>
    
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    

    This is what is happening when i perform the changes:

  • Samarth Kejriwal
    Samarth Kejriwal almost 8 years
    but how to change the color for api level 19.Is there any solution?
  • Pavneet_Singh
    Pavneet_Singh almost 8 years
    yeah you can do it but it comes on a cost.you have to manually create your own view for status bar and do the calculation for size to display it at it's appropriate place .check out this link status bar
  • Samarth Kejriwal
    Samarth Kejriwal almost 8 years
    I have tried this earlier but it is showing that the minimum api level should be 21 ..And minimum for my project is API level 14
  • Embydextrous
    Embydextrous almost 8 years
    Yes it happened with me too. android:fitsSystemWindows="true" fixes that. I have attached screenshots for the same.
  • Samarth Kejriwal
    Samarth Kejriwal almost 8 years
    i have added the command.Still not working.I have posted the screenshot above for the effect of the changes
  • Embydextrous
    Embydextrous almost 8 years
    If you are using Coordinator layout add android:fitsSystemWindows="true" to AppBarLayout and toolbar also. Else, try adding to toolbar also.
  • Embydextrous
    Embydextrous almost 8 years
    Also make sure your activity extends AppCompatActivity.
  • Samarth Kejriwal
    Samarth Kejriwal almost 8 years
    Still the same result
  • Embydextrous
    Embydextrous almost 8 years
    I have edited code. Use that since you are using Coordinator layout. Call it after super.onCreate() and before setContentView() method.
  • Konstantin
    Konstantin almost 8 years
    it should work on api level 19 or highter. No way to change status bar color on api level less then 19.
  • KlevisGjN
    KlevisGjN almost 6 years
    This is for api 21+
  • Lunchbox
    Lunchbox about 5 years
    Warns that API lv 21 is required