Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had this issue with a button with a custom background, and ended up using the <a href="http://developer.android.com/reference/android/view/View.html#setSelected%28boolean%29" rel="noreferrer">selected state</a> for this. That state is available for all views.</p> <p>To use this you have to define a custom button background as a <a href="http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList" rel="noreferrer">state list</a>:</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:state_selected="false" android:state_focused="false" android:state_pressed="false"&gt;&lt;bitmap ... /&gt;&lt;/item&gt; &lt;item android:state_selected="true"&gt;&lt;bitmap ... /&gt;&lt;/item&gt; &lt;item android:state_focused="true"&gt;&lt;bitmap ... /&gt;&lt;/item&gt; &lt;item android:state_pressed="true"&gt;&lt;bitmap ... /&gt;&lt;/item&gt; &lt;/selector&gt; </code></pre> <p>Then to use that background, let's say it is in <code>/res/drawable/button_bg.xml</code> in your layout file, you use:</p> <pre><code>... &lt;Button android:background="@drawable/button_bg" ... /&gt; ... </code></pre> <p>In your code you can switch to the (de-)selected state in your onClick listener:</p> <pre><code>myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.setSelected(true); // normal click action here } }); </code></pre> <p>The <a href="http://developer.android.com/reference/android/view/View.html#setActivated%28boolean%29" rel="noreferrer">activated state</a> matches the intended meaning better, but is only available from Android 3.x and higher.</p>
 

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