Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Status Bar background color to show colorPrimaryDark

I have a layout that used to update the background color of the status bar based on colorPrimaryDark.

This worked great when the layout's root layout was a CoordinatorLayout, but when I switched it to a LinearLayout the status bar background is no longer updated.

The source for the layout and a screenshot are pasted below. An example of a layout that works properly is also listed.

Thank you!

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".churches.ChurchesActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <FrameLayout
            android:id="@+id/contentFrame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </android.support.design.widget.CoordinatorLayout>

</LinearLayout>

styles.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>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

styles-v21.xml

<resources>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

Status Bar is not colorPrimaryDark

Status Bar is not colorPrimaryDark

Status Bar is colorPrimaryDark

Status Bar is colorPrimaryDark

like image 726
dazza5000 Avatar asked Dec 15 '15 01:12

dazza5000


People also ask

How can I change status bar background color?

Step 1: After opening the android studio and creating a new project with an empty activity. Step 2: Navigate to res/values/colors. xml, and add a color that you want to change for the status bar. Step 3: In your MainActivity, add this code in your onCreate method.

How do I change my status bar?

To customize it, first pull down the slider bar from the top of the screen. Next, tap on the three vertical dots in the top right corner. Now click on Status bar. You're in.


2 Answers

When posting styles-v21.xml I found that android:statusBarColor was set to transparent:

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

Changing android:statusBarColor to colorPrimaryDark fixed it. Thank you!

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

Not sure why statusBarColor came into play after switching to a LinearLayout from a CoordinatorLayout. Thank you!

like image 171
dazza5000 Avatar answered Oct 01 '22 02:10

dazza5000


Try this in Activity before set content view

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(getFactorColor(getResources().getColor(R.color.action_bar_color), 0.4f));
}

where getFactorColor method is

public static int getFactorColor(int color, float factor) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    hsv[2] *= factor;
    color = Color.HSVToColor(hsv);
    return color;
}
like image 33
Narendra Motwani Avatar answered Oct 01 '22 03:10

Narendra Motwani