Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make action bar icon change when clicked
    primarykey
    data
    text
    <p>I have a set of white icons on purple background in the SherlockActionBar</p> <p>I want to have them turn dark purple when being clicked on.</p> <p>I have the same icons in dark purple color, so I want to make those drawables appear on pressed state.</p> <p>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.</p> <p>I want to know how can I assign different drawable for each item's pressed state.</p> <p>Here is the code that I use right now:</p> <p>In styles.xml</p> <pre><code>&lt;style name="Theme.SherlockCustom" parent="@style/Theme.Sherlock.Light"&gt; &lt;item name="actionBarItemBackground"&gt;@drawable/action_bar_item_background&lt;/item&gt; &lt;item name="android:icon"&gt;@android:color/transparent&lt;/item&gt; &lt;item name="displayOptions"&gt;showCustom&lt;/item&gt; &lt;item name="android:minWidth"&gt;0dp&lt;/item&gt; &lt;item name="android:padding"&gt;0dp&lt;/item&gt; &lt;/style&gt; </code></pre> <p>action_bar_item_background.xml:</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@color/light_purple" android:state_pressed="true"/&gt; &lt;!-- pressed --&gt; &lt;item android:drawable="@android:color/transparent"/&gt; &lt;!-- default --&gt; &lt;/selector&gt; </code></pre> <p><strong>This is the part that doesnt work (trying to set a style that defines a new look for a particular button):</strong></p> <p>feed_icon.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;layer-list xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;item android:drawable="@drawable/ic_action_content_paste" android:right="@dimen/action_bar_icons_padding_right" android:theme="@style/feediconstyle"&gt;&lt;/item&gt; &lt;/layer-list&gt; </code></pre> <p>and style/ feediconstyle:</p> <pre><code>&lt;style name="feediconstyle" parent="@style/Theme.Sherlock.Light"&gt; &lt;item name="actionBarItemBackground"&gt;@drawable/feed_icon_background&lt;/item&gt; &lt;item name="displayOptions"&gt;showCustom&lt;/item&gt; &lt;/style&gt; </code></pre> <p>this particular icon does not obey to the new style, which is</p> <p>feed_icon_background:</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/feed_icon_purple" android:state_pressed="true"/&gt; &lt;!-- pressed --&gt; &lt;item android:drawable="@android:color/transparent"/&gt; &lt;!-- default --&gt; &lt;/selector&gt; </code></pre> <p>So how do I do it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload