Reputation: 145
I'm having a problem, I'm developing a app and I want to change background color of the buttons of the Action Bar. I have made follow it:
In my menu.xml, I have this code:
<menu 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"
tools:context=".main" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/action_search"/>
<item android:id="@+id/action_carrito"
android:title="@string/action_example"
android:icon="@drawable/ic_carrito"
app:showAsAction="always" />
</menu>
And my styles.xml, I have this:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="actionBarStyle">@style/MyActionBar</item>
<item name="actionButtonStyle">@style/MyActionButtonStyle</item>
<item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@android:color/white</item>
<item name="android:textSize">18sp</item>
</style>
<style name="MyActionButtonStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">@color/darker_green</item>
</style>
</resources>
Both buttons catch this background color. But I only want to put it for the button with id is "@+id/action_carrito". I hope somebody can to help me. Thanks in advance.
Upvotes: 1
Views: 1360
Reputation: 11982
What if you try to create a action_carrito_bg.xml
which is a layer-list
under your drawable
folder, that looks like:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/darker_green"></solid>
</shape>
</item>
<item>
<bitmap android:src="@drawable/ic_carrito"/>
</item>
</layer-list>
Then remove MyActionButtonStyle
from styles.xml
, and set that drawable as icon of the menu item:
<menu 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"
tools:context=".main" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="@string/action_search"/>
<item android:id="@+id/action_carrito"
android:title="@string/action_example"
android:icon="@drawable/action_carrito_bg"
app:showAsAction="always" />
</menu>
Upvotes: 1