Rahul kumar
Rahul kumar

Reputation: 193

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

Upvotes: 1

Views: 105

Answers (2)

user543
user543

Reputation: 3633

U can use setBackgroundResource programmatically, like:

for(int i =0; i<ab.length;i++)
{
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText(ab[i]);
XmlResourceParser parser = getResources().getXml(
            R.drawable.rbtn_textcolor_selector);
ColorStateList colors = ColorStateList.createFromXml(getResources(),
            parser);
radioButtonView.setTextColor(colors);
radioButtonView.setBackgroundResource(R.drawable.rbtn_selector);
radioGroup.addView(radioButtonView, p);

}

Upvotes: 0

Fahim
Fahim

Reputation: 12358

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);
  } 

Upvotes: 1

Related Questions