dashh
dashh

Reputation: 261

radio button reselecting not working properly in android,new in development

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

Upvotes: 0

Views: 4179

Answers (2)

Eldhose M Babu
Eldhose M Babu

Reputation: 14520

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.

Upvotes: 3

Eldhose M Babu
Eldhose M Babu

Reputation: 14520

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);
}

}

Upvotes: 1

Related Questions