How to get the Status Bar background color to show colorPrimaryDark
R

5

13

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

Reneta answered 15/12, 2015 at 1:16 Comment(1)
show us your style.xmlTrapezius
R
18

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!

Reneta answered 15/12, 2015 at 2:46 Comment(2)
CoordinatorLayout draws a status bar background when it has a top inset from system windows. By default it uses colorPrimaryDark as the drawable to draw.Limassol
Don't know how I would have figured that out! Thank you for the clarification!Reneta
C
3

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;
}
Caldera answered 15/12, 2015 at 5:36 Comment(0)
B
0

Try to add this android:fitsSystemWindows="true" property for your Linear layout and see.

Bushranger answered 15/12, 2015 at 2:14 Comment(2)
I tried this, but for some reason android:fitsSystemWindows="true" did not have the same effect when applied to a LinearLayout as it had when it was applied to CoordinateLayout. Good suggestion! Thank you!Reneta
@Shiva, this is not the correct answer, setting android:fitsSystemWindows="true" will only make LinearLayout add some paddingTop, it won't make LinearLayout draw color on the status bar.Preestablish
S
0

Add <item name="android:navigationBarColor">?attr/colorPrimaryDark</item> in your theme or call window.setNavigationBarColor(@ColorInt int color) programmatically. But, take note that navigationBarColor is introduced since Android Lollipop(API 21), which means navigationBarColor is not supported when API < 21.

Seeing answered 15/12, 2015 at 3:36 Comment(0)
H
-1

If you won't get by after trying all these. Just invalidate the cache and restart the projects. definitely this will work.

Heliogravure answered 27/3, 2017 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.