Note that there are some explanatory texts on larger screens.

plurals
  1. POImageButton state when pressed not showing up
    primarykey
    data
    text
    <p>I am trying to create a button that has a default image, but when it is pressed it switches to a new image. I have used <a href="http://vimaltuts.com/android-tutorial-for-beginners/android-custom-button" rel="nofollow noreferrer"><code>this tutorial</code></a> along with <a href="https://stackoverflow.com/questions/3996418/android-custom-image-on-off-button"><code>this question</code></a> but it will not work.</p> <p>This is my <code>info_button_selector.xml:</code></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/ic_menu_info_details2" /&gt; &lt;!-- pressed --&gt; &lt;item android:state_focused="true" android:drawable="@drawable/ic_menu_info_details2" /&gt; &lt;!-- focused --&gt; &lt;item android:drawable="@drawable/ic_menu_info_details" /&gt; &lt;!-- default --&gt; &lt;/selector&gt; </code></pre> <p>This is the code with my ImageButton:</p> <pre><code> &lt;ImageButton android:id="@+id/apModeInfoButton" android:layout_width="0dip" android:background="@null" android:layout_height="match_parent" android:layout_weight="1.15" android:clickable="true" android:src="@drawable/info_button_selector" /&gt; </code></pre> <p>The button stays at the initial state looking like <code>ic_menu_info_details</code> the entire time. </p> <p>I changed my <code>info_button_selector.xml</code> file to this:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/ic_menu_info_details2" /&gt; &lt;!-- pressed --&gt; &lt;item android:state_focused="true" android:drawable="@drawable/ic_menu_info_details2" /&gt; &lt;!-- focused --&gt; &lt;item android:drawable="@drawable/ic_menu_info_details2" /&gt; &lt;!-- default --&gt; &lt;/selector&gt; </code></pre> <p>The <code>ic_menu_info_details2</code> image was the displayed the entire time, which is expected. This shows that the button IS using the XML file as its drawable resource, but why is it that the button does not change its image when pressed?</p> <p><strong>EDIT</strong></p> <p>Using Joss's answer below, the image still does not change, and is now stretched in a strange way. Note: Both images are exactly the same except the second image is scaled to be 60% the size. This might mean that it is working but I cannot tell as the view is stretched.</p> <pre><code> &lt;ImageButton android:id="@+id/apModeInfoButton" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1.15" android:clickable="true" android:background="@drawable/info_button_selector" /&gt; </code></pre> <p>I used both of these versions of the XML file as well as different combinations of the images, all of the combinations resulted in the same sized image never changing.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/ic_menu_info_details" /&gt; &lt;!-- pressed --&gt; &lt;item android:state_focused="true" android:drawable="@drawable/ic_menu_info_details" /&gt; &lt;!-- focused --&gt; &lt;item android:drawable="@drawable/ic_menu_info_details" /&gt; &lt;!-- default --&gt; &lt;/selector&gt; &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_pressed="true" android:drawable="@drawable/ic_menu_info_details2" /&gt; &lt;!-- pressed --&gt; &lt;item android:state_focused="true" android:drawable="@drawable/ic_menu_info_details2" /&gt; &lt;!-- focused --&gt; &lt;item android:drawable="@drawable/ic_menu_info_details2" /&gt; &lt;!-- default --&gt; &lt;/selector&gt; </code></pre>
    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.
 

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