quiz_list.xml : My xml file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#43bd00"
android:textSize="20sp"
android:textStyle="bold"
android:paddingTop="4dip"
android:paddingBottom="1dip" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Radio Group"
>
<RadioButton
android:id="#+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioButton
android:id="#+id/option4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RadioGroup>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="bottom|center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/loadPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:text="Load Previous"
android:visibility="invisible" />
<Button
android:id="#+id/loadNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:text="Load Next" />
</LinearLayout>
I have a list of questions and radio buttons have the options for it.
The problems that I face are
1. When i load the next list a button is already selected.
2. When i load the previous list a different button is selected instead of the one i selected.
This is my code containing the questions as a json.
String json = "[{\"question\": \"What?\", \"option1\": \"alpha\" , \"option2\": \"beta\" , \"option3\": \"gamma\" , \"option4\": \"alpha\"},{\"question\": \"What is your name ?\", \"option1\": \"Dinesh\" , \"option2\": \"Boopesh\" , \"option3\": \"Srinath\" , \"option4\": \"JK\"},{\"question\": \"What is the capital of India?\", \"option1\": \"Delhi\" , \"option2\": \"Bombay\" , \"option3\": \"Calcutta\" , \"option4\": \"Chennai\"}]";
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray array = parser.parse(json).getAsJsonArray();
for(JsonElement obj : array) {
Quiz quiz = gson.fromJson(obj,Quiz.class);
String question = quiz.getQuestion();
System.out.println("Question: "+question);
quizList.add(quiz);
}
I'm adding the questions using a customadapter.
How can i change it to make it work ??
Any Ideas ??? Pls let me know...
Thanks Nishit.
try this
RadioGroup radioGroup;
RadioButton radioButton1;
RadioButton radioButton2;
RadioButton radioButton3;
boolean hack = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioButton1 = (RadioButton) findViewById(R.id.r1);
radioButton2 = (RadioButton) findViewById(R.id.r2);
radioButton3 = (RadioButton) findViewById(R.id.r3);
OnClickListener radioClickListener = new OnClickListener()
{
public void onClick(View v)
{
if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack)
{
radioGroup.clearCheck();
}
else
{
hack = true;
}
}
};
OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
hack = false;
}
};
radioButton1.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton2.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton3.setOnCheckedChangeListener(radioCheckChangeListener);
radioButton1.setOnClickListener(radioClickListener);
radioButton2.setOnClickListener(radioClickListener);
radioButton3.setOnClickListener(radioClickListener);
}
Related
i am developing a quiz using mobile application. currently, i am having problems on how to retrieve selected radio button from previous page. i want the selected radio button value to be displayed on the next page. below is my code in xml and java.
question1. java
public class question1 extends Activity {
private Button Button2;
public RadioButton r1,r2,r3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.question1);
r1 = (RadioButton)findViewById(R.id.rb1);
r2 = (RadioButton)findViewById(R.id.rb2);
r3 = (RadioButton)findViewById(R.id.rb3);
//Button2
Button2 = (Button) findViewById(R.id.button1);
Button2.setOnClickListener((new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(view.getContext(),question2.class);
intent.putExtra("r1",r1.isSelected());
intent.putExtra("r2",r2.isSelected());
intent.putExtra("r3",r3.isSelected());
startActivityForResult(intent,0);
}
}));
}
}
this is my code in question1.xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="452dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:src="#drawable/clay" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="YANG MANA SATUKAH DI ANTARA AYAT-AYAT BERIKUT YANG BENAR ?"
android:textAppearance="?android:attr/textAppearanceSmall" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton2"
android:text="A.Tanah liat yang leper lebih berat" />
<RadioButton
android:id="#+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton3"
android:text=" B. Kedua-dua tanah liat adalah sama berat" />
<RadioButton
android:id="#+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/radioButton2"
android:layout_marginBottom="14dp"
android:text="C.Bola tanah liat lebih berat" />
</RadioGroup>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#drawable/nextnext" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
this is my code for the second page that i want the selected radio button value to be displayed.
question2. java
public class question2 extends Activity
{
private Button Button2;
public RadioButton r1,r2,r3,r4,r5;
public RadioGroup rg1,rg2;
public String a2,b2,total;
public TextView output ;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.question2);
output = (TextView)findViewById(R.id.textView5);
r1 = (RadioButton)findViewById(R.id.rb1);
r2 = (RadioButton)findViewById(R.id.rb2);
r3 = (RadioButton)findViewById(R.id.rb3);
r4 = (RadioButton)findViewById(R.id.rb4);
r5 = (RadioButton)findViewById(R.id.rb5);
rg1 = (RadioGroup)findViewById(R.id.radioGroup1);
rg2 = (RadioGroup)findViewById(R.id.radioGroup2);
Object RadioButton;
r1= (RadioButton).getValue();
r2 =(RadioButon).getValue();
r3 =(RadioButon).getValue();
//Button2
Button2 = (Button) findViewById(R.id.button1);
Button2.setOnClickListener((new OnClickListener()
{
public void onClick(View view)
{
Intent intent = new Intent(view.getContext(),question3.class);
startActivityForResult(intent,0);
}
}
));
{
switch(rg2.getCheckedRadioButtonId()){
case R.id.rb4:
if (r4.isChecked()){
//a2 =0;
}
break;
case R.id.rb5:
if (r5.isChecked()){
//b2 = 1;
}
break;
//total = rg1.getCheckedRadioButtonId()+rg2.getCheckedRadioButtonId() ;
}}
//receive the arguments from the previous Activity
Bundle extras = getIntent().getExtras();
if (extras==null) {
return;
}
output = (TextView) findViewById(R.id.textView5);
Bundle data = this.getIntent().getExtras();
r1=data.getValue(r1);
r2=data.getValue(r2);
r3=data.getValue(r3);
double result = 0;
if (r1 == "")
{
result+ = 1;
}
else if (r2 == "true"){
result+ = 0;}
else if (r3=="true")
{
result+ = 0;
}
else {
result + =0;
}
}
}
}
question2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="452dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_weight="0.13"
android:src="#drawable/soklan2" />
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4" >
<RadioButton
android:id="#+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A. Ya" />
<RadioButton
android:id="#+id/rb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/radioGroup1"
android:layout_below="#+id/radioGroup1"
android:text="B. Tidak" />
</RadioGroup>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioGroup2"
android:layout_marginTop="20dp"
android:text="Result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#drawable/nextnext" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
I would like to know, how to retrieve the value from question 1 and displayed it on question2 page.
you could use:
r1.setChecked(getIntent().getExtras().getBoolean("r1"));
r2.setChecked(getIntent().getExtras().getBoolean("r2"));
r3.setChecked(getIntent().getExtras().getBoolean("r3"));
You may also need to change the code where you put the values:
intent.putExtra("r1",r1.isChecked());
intent.putExtra("r2",r2.isChecked());
intent.putExtra("r3",r3.isChecked());
You should also probably look at this code:
Object RadioButton;
r1 = (RadioButton).getValue();
r2 = (RadioButon).getValue();
r3 = (RadioButon).getValue();
I have no idea what you are trying to accomplish here. And it should probably be removed to keep the refferences to your RadioButton Objects
In you second activity , get the values from intent extras:
Intent intent = getIntent();
boolean r1Value = intent.getBooleanExtra("r1",false);
r1.setChecked(r1Value );
My requirement is to be able to be able to check only 1 radio button from the no of options available.
My layout is simple
User should be able to select only one of p1, p2 and p3.
The IDs for for the buttons are rB1,rB2 and rB3.
PS: I know it is possible to achieve the same with RadioGroup , but in this particular instance I want to go with Radio Button hence the query
Try this..
r1.setOnCheckedChangeListener(this);
r2.setOnCheckedChangeListener(this);
r3.setOnCheckedChangeListener(this);
And CheckedChangeListener
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (buttonView.getId() == R.id.rB1) {
r2.setChecked(false);
r3.setChecked(false);
}
if (buttonView.getId() == R.id.rB2) {
r1.setChecked(false);
r3.setChecked(false);
}
if (buttonView.getId() == R.id.rB3) {
r1.setChecked(false);
r2.setChecked(false);
}
}
}
and don't forget to implements OnCheckedChangeListener in your activity or fragment.
Try this way:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P1" />
<RadioButton
android:id="#+id/p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P2" />
<RadioButton
android:id="#+id/p3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P3" />
</RadioGroup>
Use Radio group
XML:
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P1"
android:checked="true" />
<RadioButton
android:id="#+id/rB2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P2" />
<RadioButton
android:id="#+id/rB3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="P3" />
</RadioGroup>
try below code:-
java
public class MyAndroidAppActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="#+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_male"
android:checked="true" />
<RadioButton
android:id="#+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_female" />
</RadioGroup>
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_display" />
</LinearLayout>
for more detail see below link:-
http://www.mkyong.com/android/android-radio-buttons-example/
http://www.tutorialspoint.com/android/android_radiogroup_control.htm
The solution can be achieved by setting a android:onClick="onRadioButtonClick" in the layout.xml file and creating a onRadioButtonClick method in Activity.java
public void onRadioButtonClick(View v)
{
boolean checked = ((RadioButton) v).isChecked();
RadioButton r1 = (RadioButton) findViewById(R.id.rB1);
RadioButton r2 = (RadioButton) findViewById(R.id.rB2);
RadioButton r3 = (RadioButton) findViewById(R.id.rB3);
switch(v.getId()) {
case R.id.rB1:
if (checked){
r2.setChecked(false);
r3.setChecked(false);
}
break;
case R.id.rB2:
if (checked){
r1.setChecked(false);
r3.setChecked(false);
}
break;
case R.id.rB2:
if (checked){
r1.setChecked(false);
r2.setChecked(false);
}
break;
}
I am posting this soulution because I couldn't find the solution to this query on this site(when I was looking for it earlier in the day) and had to figure it out myself. This might be useful for someone else looking for a similar solution.
I need to Create RadioButton Dynamically in a Radio Group, radio Group is already defined in the XML, after I made my research I found some topics to put the android:drawableRight="#android:drawable/btn_radio" and android:button="#null" in the XML how can I do them programmatically?
Here is my code :
final RadioGroup RG =(RadioGroup) vi.findViewById(R.id.RG);
RG.removeAllViews();
String[] answers= null;
answers=item.Answers.split(";");
final TextView txtTitle = (TextView) vi.findViewById(R.id.QuestionRow);
txtTitle.setText(item.Question);
for (int i=0;i<answers.length;i++){
RadioButton bt=null;
bt = new RadioButton(parent.getContext());
bt.setId(i+1);
bt.setText(answers[i]);
bt.setGravity(Gravity.RIGHT);
RG.addView(bt);
}
And here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Questionlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#color/White" >
<TextView
android:id="#+id/QuestionRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right"
android:textColor="#color/black_overlay"
android:textSize="30sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/RG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_below="#+id/QuestionRow"
android:layout_alignRight="#+id/QuestionRow"
></RadioGroup>
<Button
android:id="#+id/Vote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/RG"
android:text="Vote" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/RG"
android:text="See Statistics" />
</RelativeLayout>
protected void onPostExecute(HashMap<String,String> mylist) {
// TODO Auto-generated method stub
super.onPostExecute(mylist);
for(Integer z=0;z<mRG.getChildCount();z++){
View o =mRG.getChildAt(z);
((RadioButton)o).setText(text[z]);
}
for(Integer z =0;z< mRG.getChildCount();z++){
Set set = mylist.entrySet();
Iterator i = set.iterator();
View o = mRG.getChildAt(z);
NumberFormat formatter = new DecimalFormat("#0.00");
if (o instanceof RadioButton){
int counted[] = new int[ mRG.getChildCount()];
int j=0;
while(i.hasNext()) {
enter code here
Map.Entry me = (Map.Entry)i.next();
String Value=String.valueOf(formatter.format(Double.valueOf(me.getValue().toString())*100/count))+"%";
if (Integer.valueOf(me.getKey().toString())==((RadioButton) o).getId())
{
((RadioButton)o).setText(text[z]+" "+ Value);
}
}
}
}
progressDialog.hide();
}
I have a problem with my RadioButton which I declerate in my activity_main.xml:
<RadioButton
android:id="#+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/RadioButton00"
android:layout_alignBottom="#+id/RadioButton00"
android:layout_marginLeft="9dp"
android:layout_toRightOf="#+id/RadioButton00" />
<RadioButton
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/RadioButton01"
android:layout_alignBottom="#+id/RadioButton01"
android:layout_marginLeft="7dp"
android:layout_toRightOf="#+id/RadioButton01" />
Some of this RadioButtons are set programaticly to "true". But the user should have the right to make it to "false" if he clicks on it. At the moment it doesn't. If I click on it, it just still be true and does not switch to false.
public class MainActivity extends Activity {
private RadioButton RB1, RB2;
int[][] RBTrue = new int[3][16];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RB1 = (RadioButton) findViewById(R.id.RadioButton00);
RB2 = (RadioButton) findViewById(R.id.RadioButton01);
}
public void setRadioButtons()
{
steine sander = new steine();
sander.setSteine();
RBTrue = sander.getSteine();
if(RBTrue[0][0] == 1)
RB1.setChecked(true);
if(RBTrue[0][1] == 1)
RB2.setChecked(true);
}
So it's pretty easy. I gonna give an array and check it if it's 1 or not. If it is the RadioButton switching to true. But sometimes the user need to switch it back to false. At the moment it's not possible.
Declare the RadioButtons inside a RadioGroup(details here).
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="...."
android:textSize="15sp" />
<RadioButton
android:id="#+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="....."
android:textSize="15sp" />
</RadioGroup>
then inside activity,,,
RadioGroup rgGroup=(RadioGroup)findViewById(R.id.radio_group);
int selectedId = rgGroup.getCheckedRadioButtonId();
RadioButton rBtn1 =(RadioButton)findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
rBtn1.getText(), Toast.LENGTH_SHORT).show();
I'm having a set of 9 radio Buttons(a group of 3 each),user has to select 3 (1 of each group)
on right selection a increment in score while on wrong selection decrement in score,but problem is that if all 3 correct ans are selected score is coming right else not ,also it is not responding to re selecting of another radio button of the group.On submit button i have written the code, I am new in development so kindly guide
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()) {
case R.id.qa:cb.setChecked(false);
cc.setChecked(false);
cd.setChecked(false);
s[0] = false;
break;
case R.id.qb:
ca.setChecked(false);
cc.setChecked(false);
cd.setChecked(false);
s[0] = false;
break;
case R.id.qc:
cb.setChecked(false);
ca.setChecked(false);
cd.setChecked(false);
s[0] = true;
break;
}
public void onClick(View v) {
score = 0;
for (int i = 0; i < 3; i++) {
if (s[i] == true)
score++;
else
score--;
}
where s is boolean s[] = new boolean[];
I am new in development so not able to sort out problem
The code sample below will add the radiobuttons dynamically to a group so that you can arrange the buttons in any way you like using any layouts.
Layout : main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RadioButton
android:id="#+id/rbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="A" />
<RadioButton
android:id="#+id/rbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="B" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1" >
<RadioButton
android:id="#+id/rbtn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="C" />
<RadioButton
android:id="#+id/rbtn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="D" />
</LinearLayout>
</LinearLayout>
Code : MainActivity.xml
public class MainActivity extends Activity {
private static final String GROUP_A = "GroupA";
private static final String GROUP_B = "GroupB";
private List<RadioButton> radioButtons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioButton rbtn1 = (RadioButton) findViewById(R.id.rbtn1);
RadioButton rbtn2 = (RadioButton) findViewById(R.id.rbtn2);
RadioButton rbtn3 = (RadioButton) findViewById(R.id.rbtn3);
RadioButton rbtn4 = (RadioButton) findViewById(R.id.rbtn4);
addRadioButtonToGroup(rbtn1, GROUP_A);
addRadioButtonToGroup(rbtn2, GROUP_A);
addRadioButtonToGroup(rbtn3, GROUP_A);
addRadioButtonToGroup(rbtn4, GROUP_B);
}
private void addRadioButtonToGroup(RadioButton rbtn, String group) {
rbtn.setTag(group);
if (radioButtons == null) {
radioButtons = new ArrayList<RadioButton>();
}
radioButtons.add(rbtn);
rbtn.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
for (int i = 0; i < radioButtons.size(); i++) {
RadioButton radioButton = radioButtons.get(i);
if (radioButton
.getTag()
.toString()
.equalsIgnoreCase(
buttonView.getTag().toString())) {
if (!radioButton.equals(buttonView)) {
radioButton.setChecked(false);
}
}
}
}
}
});
}
}
Here..First 3 radio buttons will belong to the group A and other to Group B, which makes sure one radio button in a group can be checked at a time. We also dont need the RadioGroup and this code gives the control to arrange Radio Buttons in anyway you like.
Use RadioGroup instead of checking and un checking the RadioButtons manually.
If You put 3 radio buttons in a radio group, you will be able to check a radio button at one time, RadioGroup will manage it.
Check the sample below : On Submit button click it will calculate the score.
layout : main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="First Question" />
<RadioGroup
android:id="#+id/rg1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A" />
<RadioButton
android:id="#+id/rbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="B" />
<RadioButton
android:id="#+id/rbtn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="C" />
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Second Question" />
<RadioGroup
android:id="#+id/rg2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="P" />
<RadioButton
android:id="#+id/rbtn5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Q" />
<RadioButton
android:id="#+id/rbtn6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="R" />
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Third Question" />
<RadioGroup
android:id="#+id/rg3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/rbtn7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="X" />
<RadioButton
android:id="#+id/rbtn8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Y" />
<RadioButton
android:id="#+id/rbtn9"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Z" />
</RadioGroup>
<Button
android:id="#+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</ScrollView>
Activity :
package com.example.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private RadioGroup rg1;
private RadioGroup rg2;
private RadioGroup rg3;
private OnClickListener btnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
int answerCount = 0;
if (rg1.getCheckedRadioButtonId() == R.id.rbtn1)
answerCount++;
if (rg2.getCheckedRadioButtonId() == R.id.rbtn5)
answerCount++;
if (rg3.getCheckedRadioButtonId() == R.id.rbtn9)
answerCount++;
Toast.makeText(MainActivity.this, String.valueOf(answerCount),
Toast.LENGTH_SHORT).show();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rg1 = (RadioGroup) findViewById(R.id.rg1);
rg2 = (RadioGroup) findViewById(R.id.rg2);
rg3 = (RadioGroup) findViewById(R.id.rg3);
Button btnSumbit = (Button) findViewById(R.id.btnSubmit);
btnSumbit.setOnClickListener(btnClickListener);
}
}