Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Status Bar color for API Level below 21 is not changing

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:

like image 644
Samarth Kejriwal Avatar asked Jul 20 '16 11:07

Samarth Kejriwal


2 Answers

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

like image 79
Embydextrous Avatar answered Oct 06 '22 19:10

Embydextrous


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

like image 44
Pavneet_Singh Avatar answered Oct 06 '22 19:10

Pavneet_Singh