Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>You need to be using the <a href="http://developer.android.com/guide/topics/ui/controls/togglebutton.html" rel="nofollow">ToggleButton</a> control.</strong></p> <p>Here's the <strong>selector</strong> code you'll need to call up:</p> <p><em>(I've labelled the drawables to the states they correspond to, as per your post).</em></p> <p>*tb_selector.xml*</p> <pre><code>&lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/state4" android:state_checked="true" android:state_pressed="true" /&gt; &lt;item android:drawable="@drawable/state2" android:state_pressed="true" /&gt; &lt;item android:drawable="@drawable/state3" android:state_checked="true" /&gt; &lt;item android:drawable="@drawable/state1" /&gt; &lt;/selector&gt; </code></pre> <p><strong>Reason for the default drawable being declared last:</strong> <a href="http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList" rel="nofollow">Android applies the first item in the state list that matches the current state of the object. So, if the first item in the list contains none of the state attributes above, then it is applied every time, which is why your default value should always be last (as demonstrated in the following example).</a></p> <p><strong>Sample <a href="http://developer.android.com/guide/topics/ui/controls/togglebutton.html" rel="nofollow">ToggleButton</a> XML:</strong></p> <pre><code>&lt;ToggleButton android:id="@+id/toggleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="Vibrate on" android:textOff="Vibrate off" android:onClick="onToggleClicked" android:background="@drawable/tb_selector" /&gt; </code></pre> <p><strong>Sample <a href="http://developer.android.com/guide/topics/ui/controls/togglebutton.html" rel="nofollow">ToggleButton</a> click event:</strong></p> <pre><code>public void onToggleClicked(View view) { // Is the toggle on? boolean on = ((ToggleButton) view).isChecked(); if (on) { // Enable vibrate } else { // Disable vibrate } } </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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