Cao Linh Truong
Cao Linh Truong

Reputation: 253

Add dynamic Radiobutton into RadioGroup

public class MCQSample extends Activity implements OnClickListener{

    TextView title;
    String gotBread;
    RadioGroup AnswerRG;
    int value ;
    int value1;



    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mcqsample); 
        title = (TextView) findViewById(R.id.abc);
        nextP = (Button) findViewById(R.id.nextP);
        backP = (Button) findViewById(R.id.backP);
        AnswerRG = (RadioGroup) findViewById(R.id.AnswerRG);


            Bundle b = getIntent().getExtras();
            value = b.getInt("key", 0);

        }
    }

Hi guys, im doing the Android app and stuck on create the dynamic radio-button. Because i don't know how many button i need (which depend on the value - user input). I read some posts which can add on the Layout but i want to add into the radioGroup. Is there any ways? Thank

Upvotes: 0

Views: 3833

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

try this way to add radiobutton in RadioGroup:

private final int WC = RadioGroup.LayoutParams.WRAP_CONTENT;
RadioGroup.LayoutParams rParams;
AnswerRG = (RadioGroup) findViewById(R.id.AnswerRG);

RadioButton radioButton = new RadioButton(this);
radioButton.setText("Yellow");
radioButton.setId(1001);//set radiobutton id and store it somewhere
rParams = new RadioGroup.LayoutParams(WC, WC);
AnswerRG.addView(radioButton, rParams);

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006539

Step #1: Create a new RadioButton via its constructor and configure it as you see fit.

Step #2: Call addView() on your RadioGroup to add the RadioButton to it.

Step #3: There is no step #3

Upvotes: 5

Related Questions