user2456977
user2456977

Reputation: 3964

Changing the status bar color for Android 5.0

I've read a bunch of different posts regarding this issue but have not found anything that works. I want my app to be compatible with Android 5.0 and older versions.

For Android 5.0 I am trying to implement the status bar color change. I know this won't work for older versions though. But when I run my app on the GenyMotion VM supporting 5.0, the status bar color doesn't change to the colorPrimaryDark. It also doesn't change color in the xml preview section of Android Studio.

Here are the relevant files:

res\values\styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="AppTheme.Base">
        <!-- Customize your theme here. -->
    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColorDark</item>
        <item name="colorAccent">@color/accentColor</item>
    </style>

</resources>

res\values-v21\styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="AppTheme.Base">
        <item name="android:colorPrimary">@color/primaryColor</item>
        <item name="android:colorPrimaryDark">@color/primaryColorDark</item>
        <item name="android:colorAccent">@color/accentColor</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:statusBarColor">@color/primaryColorDark</item>
    </style>
</resources>

res\layout\toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primaryColor"
    >
</android.support.v7.widget.Toolbar>

res\layout\activity_main.xml

<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">

    <include
        android:id="@+id/tBar"
        layout="@layout/toolbar"></include>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

MainActivity.java

public class MainActivity extends ActionBarActivity
{
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar=(Toolbar)findViewById(R.id.tBar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Login");
    }
    ...
    ...
}

Possible issues:

I think I may have added some unnecessary items in res\values-v21\styles.xml. Plus I thought I was supposed to make the parent = "@android:style/Theme.Material.Light.DarkActionBar" but other tutorials have it as Base.AppTheme. Also possible that I need to add more code in MainActivity.java

Can someone please help? This whole topic has me very confused.

Thanks

Upvotes: 1

Views: 1193

Answers (4)

noobEinstien
noobEinstien

Reputation: 3293

Very simple program. you can do it programatically

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.BLUE);
}

Upvotes: 1

Ankur Chaudhary
Ankur Chaudhary

Reputation: 2769

It'll not change status bar color if you have

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

removing this line from your app theme is the only i have found yet to change status bar color t runtime using setStausbarColor().

And still if you want to change color with android:windowTranslucentStatus then this tutorial i found useful so far link.

Upvotes: 0

Simon
Simon

Reputation: 19948

You could also try changing the status bar color programmatically: https://developer.android.com/reference/android/view/Window.html#setStatusBarColor(int)

Upvotes: 1

massaimara98
massaimara98

Reputation: 7459

maybe you can find very stupid but in my code i had that experience and your v-21 style.xml should be like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryColorDark</item>
    <item name="colorAccent">@color/accentColor</item>
</style>
</resources>

also you have to didn't define status bar color in lollipop automatically its created from Dark Primary Color. You can try this code

Upvotes: 0

Related Questions