Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppCompat style background propagated to the Image within the ToolBar

Context

I'm using the newest AppCompat v7 lib (21.0.0) and I have migrated my app from ActionBar to ToolBar

Problem

  1. Images in the ToolBar automatically receive the same background as the ToolBar

Currently the SearchBox is a custom view set on the action bar (From previous implementation) I plan to switch if to the SearchView and style it accordingly but I'm still very interested in the problem I'm facing right now.

Img background

  1. When long pressing on a menu item in the ToolBar a toast appear with the hint text and the text has the same background than the ToolBar.

Toast background

How can I avoid this?

Here is my toolbar layout and the style & theme

layout v_toolbar.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:id="@+id/my_awesome_toolbar"     android:layout_width="match_parent"     android:layout_height="wrap_content"     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"     app:theme="@style/MyActionBarStyle"/> 

values/styles.xml

<style name="MyActionBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">     <item name="android:background">@color/green</item>     <item name="android:windowActionBarOverlay">true</item>      <!-- Support library compatibility -->     <item name="background">@color/green</item>     <item name="windowActionBarOverlay">true</item> </style> 

Theme

<style name="MyTheme" parent="AppBaseTheme">     <!-- Here we setting appcompat’s actionBarStyle -->     <!-- <item name="actionBarStyle">@style/MyActionBarStyle</item> -->      <item name="windowActionBar">false</item>     <!-- ...and here we setting appcompat’s color theming attrs -->     <item name="colorPrimary">@color/green</item>     <item name="colorPrimaryDark">@color/green_dark</item> </style> 

Workaround 1 for problem 1

The workaround for the first problem is to set a transparent background on the ImageView

android:background="@color/transparent" 

This does not solve the toast issue.

Solution for both problems

I set a background to the ToolBar

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:id="@+id/my_awesome_toolbar"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:background="@color/green"     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"     app:theme="@style/MyActionBarStyle"/> 

And I'm not setting the background on the theme anymore.

<style name="MyActionBarStyle" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">     <item name="android:windowActionBarOverlay">true</item>     <!-- Support library compatibility -->     <item name="windowActionBarOverlay">true</item> </style> 

As pointed by Chris Banes, a style could be created for the toolbar instead of declaring the android:background.

This fixes my problem but then I seem to have another issue with the fact that the popupTheme isn't applied and my ShareActionMenuItem drop down does not have a white background with black text. See another question here: AppCompat ToolBar popupTheme not used in the ShareAction MenuItem

Maybe this is a bug in the appcompat library.

like image 639
Benoit Avatar asked Oct 21 '14 15:10

Benoit


1 Answers

You should not be using theme like that:

  • style = local to the Toolbar
  • theme = global to everything inflated in the Toolbar

Workaround 2, as you call it, is kind of the correct way. If you want to extract some values into a style, you can do it as so:

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

Style:

<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">     <item name="android:background">@color/green</item>     <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>     <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> </style> 
like image 115
Chris Banes Avatar answered Oct 13 '22 19:10

Chris Banes