Is there any possible way to get the selected radio button
in this layout? because rg.getCheckedRadioButtonId()
not working on this layout. I can't get each of my radiobuttons ID
. It's like all my radio button
are out of my radio Group
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rg"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rad1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
<RadioButton
android:id="@+id/rad2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rad3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
<RadioButton
android:id="@+id/rad4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"/>
</LinearLayout>
</RadioGroup>
so what i did is like this, but I can only get the ID of one radiobutton
. How can I get all ID of my radiobutton
and get what is selected?.
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int radioButtonId = rg.getCheckedRadioButtonId();
if(rg.getCheckedRadioButtonId()!= -1){
RadioButton selectedans = (RadioButton) findViewById(radioButtonId);
String selectedansText = selectedans.getText().toString();
if (selectedansText == answer[position]) {
//SelectedansText match with answer
}
if(selectedansText != answer[position]) {
//selectedansText not match with answer
}
rg.clearCheck();
if (position < question.length) {
tv.setText(question[position]);
r1.setText(opts[position * 4]);
r2.setText(opts[position * 4 + 1]);
r3.setText(opts[position * 4 + 2]);
r4.setText(opts[position * 4 + 3]);
} else {
Intent intent = new Intent(grade_four_post_test.this, grade_four_post_results.class);
startActivity(intent);
}
}
else
{
Toast.makeText(grade_four_post_test.this, "Choose Answer",
Toast.LENGTH_SHORT).show();
}
}
}
);