gegobyte
gegobyte

Reputation: 5555

Why is toolbar color stuck on white?

I have implemented navigation drawer as mentioned in the official docs, here is my code:

activity_main.xml

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

<android.support.v4.widget.DrawerLayout 
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<LinearLayout
    android:id="@+id/content_linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

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

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/drawer_view"
    app:headerLayout="@layout/nav_header"/>

</android.support.v4.widget.DrawerLayout>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="titleTextColor">@color/colorWhite</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

<style name="MyButton" parent="Theme.AppCompat.Light">
    <item name="colorControlHighlight">@color/colorAccent</item>
    <item name="colorButtonNormal">@color/colorButton</item>
</style>

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

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

</resources>

I've also created styles.xml in v-21 folder and aadded 'android' prefix as one other answer on a related question suggested but that didn't work either. The color of the toolbar is still white. Here is how it looks:

enter image description here

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sampleapp.drawer">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Updated toolbar part of activity_main.xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize" />

Updated Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sampleapp.drawer">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Updated styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>
    <item name="titleTextColor">@color/colorWhite</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

Upvotes: 1

Views: 498

Answers (1)

TheWanderer
TheWanderer

Reputation: 17844

You've got the wrong theme for your Activity set in your Manifest.

In your <application> tag, change

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

to

android:theme="@style/AppTheme"

and remove the theme field from your <activity> tag.

Upvotes: 1

Related Questions