0

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?

user3290180
  • 3,590
  • 9
  • 38
  • 69

1 Answers1

1

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.

Community
  • 1
  • 1
Bojan Kseneman
  • 14,663
  • 2
  • 50
  • 58
  • then how can I get a material design? I thought AppCompat was the compatibility framework for pre-lollipop material design. Why there are such attributes if they are ignored? – user3290180 May 24 '15 at 15:12
  • As I said, pre lolipop devices don't have access to the system window to alter it, you can make a hack on kitkat, but pre KitKat, you cannot change it. I have added a video for you to understand – Bojan Kseneman May 24 '15 at 15:18
  • @user3290180 no problem – Bojan Kseneman May 24 '15 at 18:35