Search code examples
androidbackgroundactionbarsherlock

Change action background color when selected (ActionBarSherlock)


I want to change the color of the action background icon when it is pressed.

I tried the following but no results:

styles.xml

<style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
    <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
    <!-- <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item> -->
</style>

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
    <item name="actionBarItemBackground">@drawable/action_item_background</item>
    <item name="selectableItemBackground">@drawable/action_item_background</item>
</style> 

action_item_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <solid android:color="#FF0000"/>

</shape>

How can I achieve that?


Solution

  • The solution that I found was:

    action_item_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <solid android:color="#FF0000"/>
    
    </shape>
    

    action_item_background_selector.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"
            android:exitFadeDuration="@android:integer/config_shortAnimTime" >
    
        <item 
            android:state_pressed="true" 
            android:drawable="@drawable/action_item_background" />
        <item 
            android:drawable="@android:color/transparent" />
    
    </selector>
    

    styles.xml

     <style name="Theme.Styled" parent="Theme.Sherlock.Light.DarkActionBar">
                <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
                <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
                <item name="actionBarItemBackground">@drawable/action_item_background_selector</item>
                <item name="android:actionBarItemBackground">@drawable/action_item_background_selector</item>
            </style>
    
            <style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar.Solid.Inverse">
            //...
        </style>