2

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?

mobibob
  • 8,670
  • 20
  • 82
  • 131

1 Answers1

7

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">

    <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 Conradt
  • 28,420
  • 21
  • 138
  • 192
  • Where do you get bg_catlocfilter? Are they standard icons from android sdk? – mobibob Aug 01 '10 at 05:03
  • 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 Conradt Aug 01 '10 at 05:45
  • OK. I think I have it hooked up correctly, but there is no effect. Does this work in widget (my guess is 'no'). – mobibob Aug 01 '10 at 19:21
  • 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 Aug 02 '10 at 13:30