0

So I'm dynamically creating radio buttons at runtime but I want to change the background of the button. I've created a selector xml file in my drawables folder that is shown below:

<item android:drawable="@drawable/btn_radio_on_holo_light" android:state_checked="true" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@drawable/btn_radio_off_holo_light" android:state_checked="false" android:state_enabled="true" android:state_window_focused="false"/>
<item android:drawable="@drawable/btn_radio_on_pressed_holo_light" android:state_checked="true" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_radio_off_pressed_holo_light" android:state_checked="false" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_radio_on_focused_holo_light" android:state_checked="true" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/btn_radio_off_focused_holo_light" android:state_checked="false" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/btn_radio_off_holo_light" android:state_checked="false" android:state_enabled="true"/>
<item android:drawable="@drawable/btn_radio_on_holo_light" android:state_checked="true" android:state_enabled="true"/>
<!-- Disabled states -->
<item android:drawable="@drawable/btn_radio_on_disabled_holo_light" android:state_checked="true" android:state_window_focused="false"/>
<item android:drawable="@drawable/btn_radio_off_disabled_holo_light" android:state_checked="false" android:state_window_focused="false"/>
<item android:drawable="@drawable/btn_radio_on_disabled_focused_holo_light" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="@drawable/btn_radio_off_disabled_focused_holo_light" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="@drawable/btn_radio_off_disabled_holo_light" android:state_checked="false"/>
<item android:drawable="@drawable/btn_radio_on_disabled_holo_light" android:state_checked="true"/>

Does anyone know the java code that uses this drawable to update the radio button at runtime? Thanks!

Eagle's Nest
  • 1
  • 1
  • 4
  • possible duplicate of [Any way to change the color of a radio button?](http://stackoverflow.com/questions/2715950/any-way-to-change-the-color-of-a-radio-button) – CRUSADER Aug 05 '13 at 12:52

2 Answers2

-1

try this example:

    StateListDrawable mState1 = new StateListDrawable();
mState1.addState(new int[] { android.R.attr.state_pressed },
    getResources().getDrawable(R.drawable.button3_pressed));
mState1.addState(new int[] { android.R.attr.state_focused },
    getResources().getDrawable(R.drawable.button3_focused));
mState1.addState(new int[] {},
    getResources().getDrawable(R.drawable.button3_up));
myButton.setBackgroundDrawable(mState1);
akuzma
  • 1,592
  • 6
  • 22
  • 49