XML : Can I use the same"radioButton_states" drawable and create a radioGroup that has different background colors each button?

I am trying to create a radioGroup that has different colors for each of the radio buttons "unchecked" state.

Eg. enter image description here

Currently I have.

CreateRadioButtonsRow() tries to create a row of RadioButtons as a RadioGroup in a linearLayout.

   ColorChooser.java (important function from class)    private Dialog colorChooserDialog;  private Context context;    private LinearLayout linearLayoutColors;  private int[] colorChooserColors;  private Drawable radioButtonBackground;       private void CreateRadioButtonRow()  {      final RadioButton [] radioButtons = new RadioButton[6];      RadioGroup radioGroup = new RadioGroup(context);      radioGroup.setOrientation(RadioGroup.HORIZONTAL);        for (int i = 0; i < 5; i++)      {          radioButtons[i] = new RadioButton(context);          setDrawableBackgroundColor(colorChooserColors[i]);          radioButtons[i].setBackgroundResource(R.drawable.radio_button_states);          radioButtons[i].setButtonDrawable(R.drawable.null_selector);          radioGroup.addView(radioButtons[i]);      }      linearLayoutColors.addView(radioGroup);  }    private void setDrawableBackgroundColor(int color)  {      if (radioButtonBackground instanceof ShapeDrawable)      {          ((ShapeDrawable)radioButtonBackground).getPaint().setColor(color);      }      else if (radioButtonBackground instanceof GradientDrawable)      {          ((GradientDrawable)radioButtonBackground).setColor(color);      }  }    

radio_button_states.xml contains the checked and unchecked drawables.

  radio_button_states.xml    <?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/color_radio_button_enabled"/>        <item android:state_checked="false"          android:drawable="@drawable/color_radio_button_background"/>  </selector>    

color_radio_button_enabled.xml is the enabled state which is basically the unchecked circle shape with a white ring around it.

  color_radio_button_enabled.xml    <?xml version="1.0" encoding="utf-8"?>  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">      <item android:drawable="@drawable/color_radio_button_background"/>      <item android:drawable="@drawable/selected_ring"/>  </layer-list>    

Selected_Ring.xml is the white ring layer

  selected_ring.xml    <?xml version="1.0" encoding="utf-8"?>  <shape xmlns:android="http://schemas.android.com/apk/res/android"      android:shape="ring"      android:innerRadiusRatio="2"      android:thickness="2dp"      android:useLevel="false">      <size          android:width="40dp"          android:height="40dp" />      <solid          android:color="#ffffff"/>  </shape>    

color_radio_button_background.xml is a colored circle.

  <?xml version="1.0" encoding="utf-8"?>  <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">      <size android:width="40dp"            android:height="40dp"/>  </shape>    

colors.xml is used to populate the int [] colorChooserColors

  <?xml version="1.0" encoding="utf-8"?>  <resources>      <item name="c0" type="color">#ff9999</item>      <item name="c1" type="color">#ff4d4d</item>      <item name="c2" type="color">#ff0000</item>      <item name="c3" type="color">#cc0000</item>      <item name="c4" type="color">#990000</item>      <item name="c5" type="color">#660000</item>      <integer-array name="array_chooser_colors">          <item>@color/c0</item>          <item>@color/c1</item>          <item>@color/c2</item>          <item>@color/c3</item>          <item>@color/c4</item>          <item>@color/c5</item>      </integer-array>  </resources>    

What this produces is:

enter image description here

It seems that the color never gets saved and the radio_button_states only uses the last set color.

Do I need to set a different drawable button_states for each of these buttons? or is there a way I can save each colored background to each radioButton's setBackgroundResource(R.drawable.radio_button_states)?

No comments:

Post a Comment