Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBar with AppCompat actionBarItemBackground not working

I am trying to change the color of an item when it pressed. I used the actionBarItemBackground item in the action bar style, but nothing happens.. The color not changed...

Here my code:

<style name="ActionBarStyle" parent="@style/Widget.AppCompat.ActionBar.Solid">
    <item name="android:background">@color/actionbar_background</item>
    <item name="titleTextStyle">@style/ActionBarStyle.Title</item>
    <item name="actionBarItemBackground">@drawable/action_bar_item_selector</item>
    <item name="android:actionBarItemBackground" tools:ignore="NewApi">@drawable/action_bar_item_selector</item>
</style>

And the selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/pressed_item" android:state_focused="true"/>
    <item android:drawable="@drawable/pressed_item" android:state_pressed="true"/>
    <item android:drawable="@android:color/transparent"/>

</selector>

And the drawable:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#FF0000"/>
</shape>

What is worng? Someone?

like image 226
nrofis Avatar asked Dec 24 '13 20:12

nrofis


1 Answers

actionBarItemBackground isn't inherit of Widget.ActionBar. You should be placing it in your root theme, wherever you apply your ActionBarStyle, but not in your ActionBarStyle. So, something like:

<style name="Your.Theme" parent="@style/Theme.AppCompat">
    <item name="actionBarStyle">@style/ActionBarStyle</item>
    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    <item name="actionBarItemBackground">@drawable/action_bar_item_selector</item>
    <item name="android:actionBarItemBackground" tools:ignore="NewApi">@drawable/action_bar_item_selector</item>
</style>
like image 171
adneal Avatar answered Sep 27 '22 16:09

adneal