Yotam Dahan
Yotam Dahan

Reputation: 699

Toolbar background is working with mipmap and not drawable

I'm using background image in my toolbar, and from some reason the toolbar is working only with mipmap and not with drawable.

In my app_bar_main.xml file:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@mipmap/bar"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

As you can see I've used mipmap instead of drawable, though when I'm using drawable the application crashes and the following error is given:

Binary XML file line #11: Binary XML file line #16: Error inflating class android.support.v7.widget.Toolbar

Must mention this mipmap using is just making my app very slow, and it skipping frames, so I would like to change it to drawable.

How can I change the toolbar background resource of the image from mipmap to drawable?

EDIT:

Style.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>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>


    <style name="LightDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:textColor">@android:color/primary_text_light</item>
        <item name="background">@drawable/bar</item>
    </style>

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

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

</resources>

Upvotes: 1

Views: 555

Answers (3)

Radhey
Radhey

Reputation: 2239

Seems your xml file is perfect. What I suggest ,Just resize that image or prepare background image with different different size, which you want to set in to your toolbar and put it in to different drawables / mipmap folders.

android:background="@mipmap/bar"

as we know icon size should be ,

  • drawable-ldpi (120 dpi, Low density screen) - 36px x 36px
  • drawable-mdpi (160 dpi, Medium density screen) - 48px x 48px
  • drawable-hdpi (240 dpi, High density screen) - 72px x 72px
  • drawable-xhdpi (320 dpi, Extra-high density screen) - 96px x 96px

run your app again and do let me know .

Upvotes: 1

Ninja
Ninja

Reputation: 698

Please add this code in your app level build.gradle file

android {  
   defaultConfig {
     .....
     vectorDrawables.useSupportLibrary = true
    }  
 }

toolbar_default.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    style="@style/ToolBarStyle"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="@dimen/abc_action_bar_default_height_material"/>

ToolBarStyle

<style name="ToolBarStyle" parent="">
        <item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
        <item name="theme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
 </style>

Other Please remove below line from you toolbar and try to check it work or not.I am not sure but you just check it.

app:popupTheme="@style/AppTheme.PopupOverlay"

I hope this will help you

Upvotes: 0

Prabha Karan
Prabha Karan

Reputation: 1329

toolbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/MyToolbarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

Themes/Style

<style name="MyToolbarStyle">
<item name="android:maxHeight">@dimen/abc_action_bar_default_height_material</item>
<item name="android:background">@color/primary</item>
<item name="popupTheme">@style/ThemeOverlay.AppCompat.Light</item>
<item name="titleTextAppearance">@style/Theme.Toolbar.Title</item>
<!-- No need for colorPrimary, colorPrimaryDark, colorAccent here
     this should go to the AppTheme -->

Try this methos,it works..

Upvotes: 0

Related Questions