1

I have a ListView and a Button. The button should always be clickable, but the button background image should be disabled(grayed) when the ListView is empty and enabled(actual background) when the ListView has items.

I know this can be achieved from code by always setting the state enabled as true and changing the background image. But I am looking to achieve this in XML using selector.

Libin
  • 16,967
  • 7
  • 61
  • 83

1 Answers1

0

Take a look at the following code:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true" android:dither="true">
    <item android:drawable="@drawable/button_enabled" android:state_enabled="true" />
    <item android:drawable="@drawable/button_disabled" android:state_enabled="false" />
</selector>

Use this code in your drawables folder (as an XML resource). Afterwards you use this drawable and set it as a background property to your button. (android:background="@drawable/your_selector_file" without the .xml extension, of course) If you need more information just consult the following link: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

ax1mx2
  • 654
  • 1
  • 11
  • 23