Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Specify two different images for togglebutton using XML
    text
    copied!<p>I'm attempting to override the default <code>ToggleButton</code> appearance. Here's the XML that defines the <code>ToggleButton</code>:</p> <pre><code>&lt;ToggleButton android:id="@+id/FollowAndCenterButton" android:layout_width="30px" android:layout_height="30px" android:textOn="" android:textOff="" android:layout_alignParentLeft="true" android:layout_marginLeft="5px" android:layout_marginTop="5px" android:background="@drawable/locate_me"/&gt; </code></pre> <p>Now, we have two 30 x 30 icons we want to use for the clicked/non-clicked states. Right now we have code that programmatically changes the background icon depending on the state:</p> <pre><code>centeredOnLocation.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (centeredOnLocation.isChecked()) { centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on)); } else { centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me)); } } }); </code></pre> <p>Obviously I'm looking for a better way to do this. I've tried to make a selector for the background image, which would automatically switch between the states:</p> <pre><code> &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;item android:drawable="@drawable/locate_me" /&gt; &lt;!-- default --&gt; &lt;item android:state_checked="true" android:drawable="@drawable/locate_me_on" /&gt; &lt;!-- pressed --&gt; &lt;item android:state_checked="false" android:drawable="@drawable/locate_me" /&gt; &lt;!-- unchecked --&gt; </code></pre> <p></p> <p>But this does not work; reading the <code>ToggleButton</code> API (<a href="http://developer.android.com/reference/android/widget/ToggleButton.html" rel="noreferrer">http://developer.android.com/reference/android/widget/ToggleButton.html</a>), it appears that the only inherited xml attributes are</p> <pre><code> XML Attributes Attribute Name Related Method Description android:disabledAlpha The alpha to apply to the indicator when disabled. android:textOff The text for the button when it is not checked. android:textOn The text for the button when it is checked. </code></pre> <p>There does not seem to be the android:state_checked attribute, despite the class having the method <code>isChecked()</code> and <code>setChecked()</code>.</p> <p>So, is there a way to do what I want in XML, or am I stuck with my messy workaround?</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