Search code examples
androidandroid-layoutcolorsstylesstatusbar

Set the StatusBar color through theme.?


I am going to change the statusbar color through the styles.xml but can't set the statusbar color. I don't want to set the color of status bar through the coding, i only want through the theme/style.

Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="eazitouk.eazitoclient.mainclasses.TestActivity">

    <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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_test" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

Manifest.xml

<application
        android:name="eazitouk.eazitoclient.miscs.AppController"
        android:icon="@drawable/ic_launchicon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".TestActivity"
            android:label="@string/title_activity_test"
            android:theme="@style/AppTheme1">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

color.xml

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

    <color name="ColorPrimary">#df4a43</color>
    <color name="ColorPrimaryDark">#c8423c</color>

    <color name="ColorPrimary1">#FFCF3B</color>
    <color name="ColorPrimaryLight">#F3A2A0</color>
    <color name="ColorPrimaryLightShade">#e56e66</color>

</resources>

Style.xml

<resources>

    <!-- Application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
    </style>

    <style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <item name="android:textColorSecondary">@color/accentcolor</item>-->
    </style>
</resources>

This is my style.xml and AppTheme is apply to application and AppTheme1 Apply to my activity but can't set the statusbar color


Solution

  • I have tested your code. Now Change to this you have problem in your style. So follow my way to get Output.

    Android manifest.xml.

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:largeHeap="true"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
    
            <activity
                android:name=".TestActivity"
                android:label="@string/title_activity_test"
                android:theme="@style/AppTheme">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    

    colors.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <color name="primaryColor">#EF6C00</color>
        <color name="primaryColorDark">#E65100</color>
        <color name="accentcolor">#0072BA</color>
    
    </resources>
    

    styles.xml

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/primaryColor</item>
            <item name="colorPrimaryDark">@color/primaryColorDark</item>
            <item name="colorAccent">@color/accentcolor</item>
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</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" />
    
    
    </resources>
    

    styles.xml(v21)

    <?xml version="1.0" encoding="utf-8"?>
    <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>
    
    </resources>
    

    finally Output :

    enter image description here

    and I more thing Keep in mind that You have set targetSdkVersion 23.