user3290180
user3290180

Reputation: 4410

How to color the status bar with styles attributes in all Android versions?

This is styles.xml

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/accent</item>
    <!-- Other attributes -->
</style>

While this is v21 styles.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:colorPrimary">@color/primary</item>    
    <item name="android:colorPrimaryDark">@color/primaryDark</item>
    <item name="android:colorAccent">@color/accent</item>
</style>
</resources>

I'm using that parent theme because in activity.java I'm creating The Toolbar and setting it as the Toolbar for the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
}

On Lollipop versions it's fine but it's not working at all when I deploy on a device with API lower than v21, I still see the black status bar and the primaryDark is totally ignored. Is it the right way?

Upvotes: 0

Views: 1873

Answers (1)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

On Lollipop versions it's fine but it's not working at all when I deploy on a device with API lower than v21, I still see the black status bar and the primaryDark is totally ignored. Is it the right way?

Your primary dark is ignored on all pre-lolipop devices as this is a feature since Android Lolipop (5.0+). The status bar is a system window owned by the operating system. On pre-5.0 Android devices, applications do not have permission to alter its color, this is not something the AppCompat library can alter.

There is a "hack" on the KitKat as described here

You need to make the status bar transculent

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

And you need to apply this to your toolbar to make the status bar more darker

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    ...
    android:fitsSystemWindows="true"/>

You can also watch this video about it.

I am afraid that trying to change the color on pre KitKat devices is impossible, tough I would love to be corrected.

Upvotes: 1

Related Questions