7

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:

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50
Samarth Kejriwal
  • 1,168
  • 2
  • 15
  • 30

5 Answers5

6

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

Embydextrous
  • 1,611
  • 1
  • 12
  • 20
2

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

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • 1
    but how to change the color for api level 19.Is there any solution? – Samarth Kejriwal Jul 20 '16 at 11:22
  • 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](http://stackoverflow.com/questions/22192291/how-to-change-the-status-bar-color-in-android) – Pavneet_Singh Jul 20 '16 at 11:28
0

For your information there will be no default classes or methods for status bar color changes under API 21.

Still you want to implement for your requirement then you can apply play with layout and style.

Make value folder for api version i.e. values-v19

<!-- 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="android:windowTranslucentNavigation">false</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

Now need to change in layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="moon.testwithold.MainActivity">
    <RelativeLayout
        android:id="@+id/statusbar"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimaryDark" >
    </RelativeLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</LinearLayout>

Now need to change in main class

public class MainActivity extends AppCompatActivity {
    RelativeLayout statusbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        statusbar=(RelativeLayout)findViewById(R.id.statusbar);
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT)
        {
            int titleBarHeight= getStatusBarHeight();
            Log.e("TAG"," titleBarHeight "+titleBarHeight);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,titleBarHeight);
            statusbar.setLayoutParams(params);
            statusbar.setVisibility(View.VISIBLE);
        }
        else
        {
            statusbar.setVisibility(View.GONE);
        }
    }
    public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}

Note:

If you want to do smart work then please make global activity class(Base activity) and implement above method in that class and extend that class at all activity so you can do smart work and reduce coding.

ViramP
  • 1,659
  • 11
  • 11
-1

is change status bar color for api level below 21

public void setStatusBarColor(Activity activity, int RecColor) {
        Window window = activity.getWindow();
        // clear FLAG_TRANSLUCENT_STATUS flag:
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        // finally change the color
        window.setStatusBarColor(ContextCompat.getColor(activity, RecColor));
    }
hamid
  • 171
  • 1
  • 1
  • 9
-2

try to use

<item name="android:statusBarColor">@color/color_name</item>

inside you style.

Konstantin
  • 174
  • 7