Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of your approach, Android has something called selectors that can change the background resource automatically depending on the state of the view. Selectors are defined in a separate drawable xml file and referenced from the xml declaration of the view that utilizes the states. For example: Selector (android_button.xml):</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:drawable="@drawable/android_pressed" android:state_pressed="true" /&gt; &lt;item android:drawable="@drawable/android_focused" android:state_focused="true" /&gt; &lt;item android:drawable="@drawable/android_normal" /&gt; &lt;/selector&gt; </code></pre> <p>Button:</p> <pre><code>&lt;Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:background="@drawable/android_button" /&gt; </code></pre> <p>You will still have to implement a clicklistener for each button, but you will have a selector that you can reuse in all the buttons you want, and the presentation logic has been separated from the business logic of your app.</p> <p>The source code shown in this answer comes straight from <a href="http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton" rel="nofollow">http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton</a> where you can read a more complete explanation. </p>
    singulars
    1. This table or related slice is empty.
    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.
    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