Reputation: 109
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.
Upvotes: 2
Views: 4576
Reputation: 831
the problem is this line. you didn't send where is this code belonged to. when you get out of this scope and come back you will create new RadioGroup
. You should have one and only one RadioGroup so the RadioButton
s have same RadioGroup and by default one of them will be selected. To this, you can define RadioGroup in an XML and use this to define variable for it RadioGroup rg = (RadioGroup) findViewById(R.id.YOURNAME);
or you can define your RadioGroup as Instance variable(Global).
final RadioGroup rg=new RadioGroup(Survay_MainActivity.this);
Upvotes: 0
Reputation: 1176
I faced the same problem. The cause is the RadioGroup's id is same as one of RadioButtons
Upvotes: 0
Reputation: 89
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.
Upvotes: 6
Reputation: 1325
Your problem comes from the fact that you are setting an id for your items manually
rg.setId(Integer.valueOf(entry1.getKey()));
btn.setId(i);
Android works with id's generated automatically that are different. Setting the id's manually it might happen that you give the same ID to the radio group and the radio button.
I used your code and set the group to ID 3 and radios from 0 to 4.
Needless to say that after I click the button with id 3 it always stays on because the group has the same id.
So, try to remove the id setting part, or, if you insist make sure you always have distinct id's.
Upvotes: 1
Reputation: 877
There are some things misplaced these changes you have to make out.
1.Change your OnClickListener
by OnCheckChangeListener
2.Clear check your radioGroup
after adding all radioButtons
and before adding it in LinearLayout
.
Upvotes: 2