26
votes

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.

2
You can try the second workaround. It solves the problem but bring another one if you want Dark ActionBar & Light dropdownBenoit
So I'm gonna need to manage 2 different themes for the real actionbar (when used) and for the toolbar (whenever needed)?nadavfima
No you get rid of the old actionbar, you only use ToolBar nowBenoit
Would this mean I need to add a toolbar to every activity now (via XML or java)? feels kind of oddnadavfima
yes, put it in its own xml layoutBenoit

2 Answers

49
votes

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>
5
votes

Never use themes to edit. Because theme defines overall app behavior. U can use two methods for that

Method:1

U can directly specify the parameters in appbar.xml as shown below

in appbar.xml

  <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/green"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme=">style/ThemeOverlay.AppCompat.Light"/>

Method:2

you can use a style as Chris_Banes suggested in above post

in appbar.xml:

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

in 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>

Points To Note

With the new Toolbar you can apply a style and a theme. They are different! The style is local to the Toolbar view, for example the background color. The app:theme is instead global to all ui elements inflated in the Toolbar, for example the color of the title and icons.