views:

41

answers:

1

I am using the Android SDK icon-button for refresh (ic_menu_refresh) in a widget and I need to change the selection state when it is pressed. How is this done? Do I define an XML for the button?

+3  A: 

You define the different states in xml via selector.

Sample (esp. see the state-attributes):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;

    <item android:drawable="@drawable/bg_catlocfilter" android:state_pressed="false" />
    <item android:drawable="@drawable/bg_catlocfilter_dark" android:state_pressed="true" />
    <item android:drawable="@drawable/bg_catlocfilter" android:state_focused="false" />
    <item android:drawable="@drawable/bg_catlocfilter_dark" android:state_focused="true" />

</selector>
Mathias Lin
Where do you get bg_catlocfilter? Are they standard icons from android sdk?
mobibob
No, that's just an example from my app. You replace it with your icons (i.e. ic_menu_refresh) that you're using. You create one normal and one highlighted one.
Mathias Lin
OK. I think I have it hooked up correctly, but there is no effect. Does this work in widget (my guess is 'no').
mobibob
@mathias, my question was intended for widget, but I did not make that clear. I accept your answer for the question I asked, but I need to re-ask as a widget.
mobibob