Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make action bar icon change when clicked

Tags:

android

I have a set of white icons on purple background in the SherlockActionBar

I want to have them turn dark purple when being clicked on.

I have the same icons in dark purple color, so I want to make those drawables appear on pressed state.

Now, I know how to do this in the whole application theme, but it means that I have to use the same drawable for all the icons.

I want to know how can I assign different drawable for each item's pressed state.

Here is the code that I use right now:

In styles.xml

<style name="Theme.SherlockCustom" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarItemBackground">@drawable/action_bar_item_background</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="displayOptions">showCustom</item>
    <item name="android:minWidth">0dp</item>
    <item name="android:padding">0dp</item>
</style>

action_bar_item_background.xml:

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

    <item android:drawable="@color/light_purple" android:state_pressed="true"/>
 <!-- pressed -->
    <item android:drawable="@android:color/transparent"/>
 <!-- default -->

</selector>

This is the part that doesnt work (trying to set a style that defines a new look for a particular button):

feed_icon.xml:

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

    <item
        android:drawable="@drawable/ic_action_content_paste"
        android:right="@dimen/action_bar_icons_padding_right"
        android:theme="@style/feediconstyle"></item>


</layer-list>

and style/ feediconstyle:

<style name="feediconstyle" parent="@style/Theme.Sherlock.Light">
    <item name="actionBarItemBackground">@drawable/feed_icon_background</item>
    <item name="displayOptions">showCustom</item>
</style>

this particular icon does not obey to the new style, which is

feed_icon_background:

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

    <item android:drawable="@drawable/feed_icon_purple" android:state_pressed="true"/>
 <!-- pressed -->
    <item android:drawable="@android:color/transparent"/>
 <!-- default -->

</selector>

So how do I do it?

like image 754
Kaloyan Roussev Avatar asked Feb 16 '23 00:02

Kaloyan Roussev


1 Answers

I'd like to qualify with "I have not checked this using ActionBarSherlock only the standard actionbar"

That said I know this will work on the standard actionbar and see no reason why it would not in ActionBarSherlock.

In the onCreate of the activity I have (I'm using the logo attribute but it will work for MenuItems as well)

    getActionBar().setLogo(R.drawable.logo);
    getActionBar().setDisplayUseLogoEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

In logo.xml I have a selector

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" 
          android:drawable="@drawable/logopressed"/>
    <item android:drawable="@drawable/logonotpressed" />
</selector>

In logopressed.xml A layer list can be used to make the background

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@android:color/holo_purple" />
    <item
        android:drawable="@drawable/logo_normal" />   
</layer-list>

In logonotpressed.xml switch out the background colour and/or icon drawable as desired

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@android:color/white" />
    <item
        android:drawable="@drawable/logo_inverted" />   
</layer-list>

So the crux of this solution is to set the selector drawable and let it select a whole new drawable for each state rather than setting a selector drawable to the background attribute of a otherwise static variable

Note: you should be able to theme the items with a background instead of doing 2 layers but I haven't been able to get this to work

like image 153
Tristan Burnside Avatar answered Feb 28 '23 12:02

Tristan Burnside