Search code examples
androidandroid-radiogroupandroid-radiobutton

how to add drawable xml file into radiobutton that is created using For loop


i know it is silly question but i m searching from 2 days and not able to get answer of this question i customized RadioButton so it will look good and it is working while i used drawable file into layout but i do not how to use/ get these files into my code. Right now RadioButton is creating using For Loop
here is how i m creating RadioButton

for(int i =0; i<ab.length;i++)
  {
    RadioButton radioButtonView = new RadioButton(this);
    radioButtonView.setText(ab[i]);
    radioGroup.addView(radioButtonView, p);
  } 

but i can use xml files in layout like this

<RadioButton
    android:id="@+id/radioAndroid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/rbtn_selector"
    android:textColor="@drawable/rbtn_textcolor_selector" />

can anyone suggest how to get these files into my for loop so that RadioButton will looks good thanks


Solution

  • use this

    for(int i =0; i<ab.length;i++)
      {
        RadioButton radioButtonView = new RadioButton(this);
        radioButtonView.setText(ab[i]);
        radioButtonView.setTextColor(R.color.color_name);
         radioButtonView.setBackGroundResource(R.drawable.rbtn_selector);
        radioGroup.addView(radioButtonView, p);
      }