-1

I want to make a quiz app. One question has 7 radio button. Before this, I use onClick method for every radio button. The problem is user can click more than one radio button in one question (I put radio button in radio group). Now I want to use RadioGroup onCheckedChangeListener but the problem still the same. How to fix that?

This is my code for activity:

public class MainActivity extends AppCompatActivity {

int score = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final RadioGroup question1_radioGroup = (RadioGroup) findViewById(R.id.question1_radiogroup);
    question1_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            View radioButton = question1_radioGroup.findViewById(checkedId);
            boolean checked = ((RadioButton) radioButton).isChecked();
            switch(radioButton.getId()) {
                case R.id.question1_radioButton1:
                    if(checked) {score = score + 1;}
                    break;
                case R.id.question1_radioButton2:
                    if(checked) {score = score + 2;}
                    break;
                case R.id.question1_radioButton3:
                    if(checked) {score = score + 3;}
                    break;
                case R.id.question1_radioButton4:
                    if(checked) {score = score + 4;}
                    break;
                case R.id.question1_radioButton5:
                    if(checked) {score = score + 5;}
                    break;
                case R.id.question1_radioButton6:
                    if(checked) {score = score + 6;}
                    break;
                case R.id.question1_radioButton7:
                    if(checked) {score = score + 7;}
                    break;
            }

        }
    });

    final RadioGroup question2_radioGroup = (RadioGroup) findViewById(R.id.question2_radiogroup);
    question2_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            View radioButton = question1_radioGroup.findViewById(checkedId);
            boolean checked = ((RadioButton) radioButton).isChecked();
            switch(radioButton.getId()) {
                case R.id.question2_radioButton1:
                    if(checked) {score = score + 1;}
                    break;
                case R.id.question2_radioButton2:
                    if(checked) {score = score + 2;}
                    break;
                case R.id.question2_radioButton3:
                    if(checked) {score = score + 3;}
                    break;
                case R.id.question2_radioButton4:
                    if(checked) {score = score + 4;}
                    break;
                case R.id.question2_radioButton5:
                    if(checked) {score = score + 5;}
                    break;
                case R.id.question2_radioButton6:
                    if(checked) {score = score + 6;}
                    break;
                case R.id.question2_radioButton7:
                    if(checked) {score = score + 7;}
                    break;
            }
        }
    });

    final RadioGroup question3_radioGroup = (RadioGroup) findViewById(R.id.question3_radiogroup);
    question3_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            View radioButton = question1_radioGroup.findViewById(checkedId);
            boolean checked = ((RadioButton) radioButton).isChecked();
            switch(radioButton.getId()) {
                case R.id.question3_radioButton1:
                    if(checked) {score = score + 1;}
                    break;
                case R.id.question3_radioButton2:
                    if(checked) {score = score + 2;}
                    break;
                case R.id.question3_radioButton3:
                    if(checked) {score = score + 3;}
                    break;
                case R.id.question3_radioButton4:
                    if(checked) {score = score + 4;}
                    break;
                case R.id.question3_radioButton5:
                    if(checked) {score = score + 5;}
                    break;
                case R.id.question3_radioButton6:
                    if(checked) {score = score + 6;}
                    break;
                case R.id.question3_radioButton7:
                    if(checked) {score = score + 7;}
                    break;
            }
        }
    });

    final Button generate_result_button = findViewById(R.id.jana_keputusan_button);
    generate_result_button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent generate_result_intent = new Intent(MainActivity.this, ResultActivity.class);
            generate_result_intent.putExtra("score", score);
            startActivity(generate_result_intent);
        }
    });
}


}

This is my xml file:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/instruction_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/instruction" />

// Question 1
<TextView
    android:id="@+id/question1_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question1" />

<RadioGroup
    android:id="@+id/question1_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question1_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <RadioButton
            android:id="@+id/question1_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <RadioButton
            android:id="@+id/question1_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

        <RadioButton
            android:id="@+id/question1_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <RadioButton
            android:id="@+id/question1_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <RadioButton
            android:id="@+id/question1_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />

        <RadioButton
            android:id="@+id/question1_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 2
<TextView
    android:id="@+id/question2_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question2" />

<RadioGroup
    android:id="@+id/question2_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question2_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <RadioButton
            android:id="@+id/question2_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <RadioButton
            android:id="@+id/question2_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

        <RadioButton
            android:id="@+id/question2_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <RadioButton
            android:id="@+id/question2_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <RadioButton
            android:id="@+id/question2_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />

        <RadioButton
            android:id="@+id/question2_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

// Question 3
<TextView
    android:id="@+id/question3_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/question3" />

<RadioGroup
    android:id="@+id/question3_radiogroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/question3_radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <RadioButton
            android:id="@+id/question3_radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <RadioButton
            android:id="@+id/question3_radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />

        <RadioButton
            android:id="@+id/question3_radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="4" />

        <RadioButton
            android:id="@+id/question3_radioButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="5" />

        <RadioButton
            android:id="@+id/question3_radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="6" />

        <RadioButton
            android:id="@+id/question3_radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="7" />

    </LinearLayout>
</RadioGroup>

<Button
    android:id="@+id/jana_keputusan_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Jana Keputusan" />

</LinearLayout>

1 Answers1

0

It's not working because of LinearLayout inside RadioGroup. All RadioButtons are not grouped together because of LinearLayout between them.

RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.

Just change your code like this it will work :

<RadioGroup
android:id="@+id/question1_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation='horizontal'>


    <RadioButton
        android:id="@+id/question1_radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1" />

    <RadioButton
        android:id="@+id/question1_radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2" />

    <RadioButton
        android:id="@+id/question1_radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3" />

    <RadioButton
        android:id="@+id/question1_radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="4" />

    <RadioButton
        android:id="@+id/question1_radioButton5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="5" />

    <RadioButton
        android:id="@+id/question1_radioButton6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="6" />

    <RadioButton
        android:id="@+id/question1_radioButton7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="7" />

Hope it helps.

Sahil
  • 952
  • 1
  • 7
  • 14