8

I am developing with the latest SDK version (API 21) and Support Library 21.0.2 and I've been having trouble when trying to implement the new Material Design guidelines.

Material Design says that I need to have my primary color and my accent color and apply them over my app. But sometimes when I open the app the primary color becomes transparent in some widgets, it goes back to normal until I close the app (with the Back Button) and launch it again.

Here is an example of the primary color being transparent in my toolbar.

enter image description here

I am using Teal 500 as my primary color and, as you can see, it is transparent only in the android.support.v7.widget.Toolbar. This also happens on my Navigation Drawer and (sometimes , sometimes not) in another random widgets.

This is my Toolbar

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

I've tried with @color/primary and ?attr/colorPrimary with no success.

Here is my Theme (I don't know if it is related, but just in case):

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>     
    <item name="colorAccent">@color/accent</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:textColorPrimary">#fff</item>

    <item name="windowActionModeOverlay">true</item>
    <item name="android:textViewStyle">@style/Widget.TextView.White</item>
</style>

This just happens with the primary color, accent color works fine. My device is running 4.2.2 and I haven't checked on more devices.

Activity with Toolbar

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

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

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="MergeRootFrame" />
    </LinearLayout>

    <!-- The navigation drawer -->

    <LinearLayout
        android:id="@+id/navdrawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:orientation="vertical" >

        <!-- IN THIS IT ALSO HAPPENS -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingBottom="16dp"
            android:orientation="vertical"
            android:background="@color/primary" >

            <!-- Some stuff -->

        </LinearLayout>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:background="@color/black_light"
            android:divider="@color/grey"
            android:choiceMode="singleChoice" />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
peguerosdc
  • 946
  • 11
  • 21
  • could you please add your activity's xml where you added this Toolbar? – Nguyễn Hoài Nam Nov 30 '14 at 05:20
  • @NguyễnHoàiNam done, please see my edit. Thanks in advance and I think this is not related to the Activity itself because it happens in another activities too. – peguerosdc Nov 30 '14 at 05:28
  • well since your theme is correct (NoActionBar and colorPrimary attribute, etc), I want to make sure that you don't have your values-v11 styles and values-v14 styles defined. It may override you recent theme on your 4.2.2 devices. And assume you don't have any programmatically modification to your Toolbar (in code), I will try to test your code on my device. – Nguyễn Hoài Nam Nov 30 '14 at 06:10
  • @NguyễnHoàiNam Thanks, and no, this theme is in `values` and `values-21` is empty, currently I have no more `values-vX` folders. I am following the [official Navigation Drawer guide](http://developer.android.com/training/implementing-navigation/nav-drawer.html) and I am not touching the Toolbar in other methods. Just to say that the Toolbar was an example, but this occurs in more random widgets (i.e. a TextView). – peguerosdc Nov 30 '14 at 06:20
  • It's weird (well, as expected). Consider to reference this question? http://stackoverflow.com/questions/2118251/theme-style-is-not-applied-when-inflater-used-with-applicationcontext . If it doesn't help, I would like to know your manifest (where did you use your theme, in Application tag or in Activity tag), your activity class where the weird behaviour appears, your Widget.TextView.White definition too. But consider the right way to use context first. – Nguyễn Hoài Nam Nov 30 '14 at 06:48
  • @NguyễnHoàiNam well, that Context thing was something I didn't know. I have changed a few of these misuses and I will keep playing with my app, if this behaviour stops/continues I'll let you know. Thanks. – peguerosdc Nov 30 '14 at 07:11
  • @NguyễnHoàiNam thought it was solved but no, it just happened again. I think it may be a bug as said below. – peguerosdc Dec 02 '14 at 04:18

2 Answers2

4

The problem is related with the way the toolbar's background is handled. It is shared between all instances of toolbar, so if you tend to change the toolbar's background alpha, you change the alpha of the drawable that is reused on other screens too.

Make sure you are not manipulating with the Toolbar's background properties, instead set the new background color/drawable each time you want to customize it.

  • Wow! I wasn't aware of that Toolbar's functionality! I currently have no access to that app but actually I was manipulating the Toolbar's alpha in another Activity, so this is probably the right answer (maybe someone can confirm it). It would be interesting to know how you discovered this. – peguerosdc May 07 '15 at 04:51
  • 2
    based on the [documentation](http://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()): By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification. – Andrzej Chmielewski May 08 '15 at 06:22
  • saves my day , tanks bro – Mohsen Navabi Nov 16 '16 at 08:18
0

Maybe these Support Library bugs are related to your issue.

https://code.google.com/p/android/issues/detail?id=78289
https://code.google.com/p/android/issues/detail?id=78346

Sadly, still not fixed in 21.0.2

Antonio Jose
  • 2,778
  • 3
  • 21
  • 26
  • I think it is a bug, the first ticket looks pretty much the same as my case and I found [kind of another one](https://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=transparent&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=81043). – peguerosdc Dec 02 '14 at 04:20
  • Yeah, they all look like be related. I hope the fix them soon because it has passed like one month since I'm waiting... – Antonio Jose Dec 02 '14 at 08:58