Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two radio buttons getting selected in Radio Group

enter image description here

Two buttons getting selected in the radio group.
I do not know where I am getting wrong. Please help me out.

final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);
rg.clearCheck();
rg.setId(Integer.valueOf(entry1.getKey()));  
Log.v("rg getid", "rg"+rg.getId());
for(int i =0;i<values.size();i++){
    // Create Button
    final RadioButton btn = new RadioButton(Survay_MainActivity.this);
    btn.setId(i);
    btn.setTextColor(Color.parseColor("#000000"));
    btn.setBackgroundColor(Color.TRANSPARENT);
    btn.setGravity(Gravity.LEFT);
    btn.setText(values.get(i));

    rg.addView(btn);
    btn.setLayoutParams(params);
    btn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            JSONObject quesAns = new JSONObject();
            String ans=btn.getText().toString().trim();
            try {
                quesAns.put(String.valueOf(rg.getId()), ans);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            jsonarray.put(quesAns);    
            Log.v("jsonarray", "jsonarray"+jsonarray);
        }
    });
}     
views.addView(rg);     

1) I am creating the RadioGroup out of the loop.

2) Adding radio button to the RadioGroup in the for loop

3) When the loop finishes the RadioGroup is added to the linerlayout.

like image 610
user2332087 Avatar asked Dec 20 '25 02:12

user2332087


1 Answers

You just need to change different ids for different radio button. There may be some id clash in gen file. Radio button1 : android:id="@+id/one Radio button2 : android:id="@+id/two" Radio button3 : android:id="@+id/three"

Hope this would help.

like image 80
Mohammad Kamran Avatar answered Dec 22 '25 17:12

Mohammad Kamran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!