2

I want to create a selector using the code below:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/sh_radio_icon_checked" />
    <item android:state_checked="false" android:drawable="@drawable/sh_radio_icon_unchecked" />
</selector>

So far I have managed to add the first item, like this:

StateListDrawable drawable = new StateListDrawable();
int[] sFocusedSelected = {android.R.attr.state_checked};
Drawable dFocusedSelected = getResources().getDrawable(R.drawable.sh_radio_icon_checked);
drawable.addState(sFocusedSelected, dFocusedSelected);

But do I add the state_checked=false since there's no state_unchecked constant?

Anoop Kanyan
  • 618
  • 7
  • 19
rbasniak
  • 4,484
  • 11
  • 51
  • 100

1 Answers1

2

Observe "-" (Minus/Hyphen) in the beginning of "android.R.attr.state_checked"

sld.addState(new int[] {-android.R.attr.state_checked  },  greyD);

Android : How to update the selector(StateListDrawable) programmatically

Community
  • 1
  • 1
takrishna
  • 4,884
  • 3
  • 18
  • 35