0

I have been trying to make a quizzing app using RadioGroups for each question having 4 options. I have made the app to calculate the score of the user by maintaining a pointer for both the correct and incorrect answers selected. For example, in my first activity, there are 8 questions, each with its RadioGroups for answers. If the user selects the correct option the pointer for the correct answer is incremented, and the same happens for incorrect answers. In the end, the final score of that activity is calculated and is intented to the next activity.

Code for entire MainActivity:

package com.example.myapplicationthreesubjectquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.strictmode.WebViewMethodCalledOnWrongThreadViolation;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public int correctAnsBio=0, wrongAnsBio=0;
    public RadioGroup rgbq1, rgbq2, rgbq3, rgbq4, rgbq5, rgbq6, rgbq7, rgbq8;
    public RadioButton rbbq1, rbbq2, rbbq3, rbbq4, rbbq5, rbbq6, rbbq7, rbbq8;
    public int corbq1, corbq2, corbq3, corbq4, corbq5, corbq6, corbq7, corbq8;

    Button moveToChemistry;

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

        rgbq1 = findViewById(R.id.rgbq1);
        rgbq2 = findViewById(R.id.rgbq2);
        rgbq3 = findViewById(R.id.rgbq3);
        rgbq4 = findViewById(R.id.rgbq4);
        rgbq5 = findViewById(R.id.rgbq5);
        rgbq6 = findViewById(R.id.rgbq6);
        rgbq7 = findViewById(R.id.rgbq7);
        rgbq8 = findViewById(R.id.rgbq8);

        moveToChemistry = findViewById(R.id.moveToChemistry);

        corbq1 = 1;
        corbq2 = 1;
        corbq3 = 1;
        corbq4 = 1;
        corbq5 = 1;
        corbq6 = 1;
        corbq7 = 1;
        corbq8 = 1;



        moveToChemistry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final int scoreInBio = (4*correctAnsBio)+((-1)*wrongAnsBio);
                Intent intent = new Intent(MainActivity.this, com.example.myapplicationthreesubjectquiz.chemistry.class);
                intent.putExtra("bioScore", scoreInBio);
                startActivity(intent);

            }
        });


    }

    public void clickedBq1(View v){

        int selected = -1;

        switch(rgbq1.getCheckedRadioButtonId())
        {
            case R.id.rbbq1o1:
                selected = 1;
                break;
            case R.id.rbbq1o2:
                selected = 2;
                break;
            case R.id.rbbq1o3:
                selected = 3;
                break;
            case R.id.rbbq1o4:
                selected = 4;
                break;
        }

        if(selected == corbq1)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq1)
        {
            wrongAnsBio++;
        }


    }
    public void clickedBq2(View v){

        int selected = -1;

        switch(rgbq2.getCheckedRadioButtonId())
        {
            case R.id.rbbq2o1:
                selected = 1;
                break;
            case R.id.rbbq2o2:
                selected = 2;
                break;
            case R.id.rbbq2o3:
                selected = 3;
                break;
            case R.id.rbbq2o4:
                selected = 4;
                break;
        }

        if(selected == corbq2)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq2)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq3(View v){

        int selected = -1;

        switch(rgbq3.getCheckedRadioButtonId())
        {
            case R.id.rbbq3o1:
                selected = 1;
                break;
            case R.id.rbbq3o2:
                selected = 2;
                break;
            case R.id.rbbq3o3:
                selected = 3;
                break;
            case R.id.rbbq2o4:
                selected = 4;
                break;
        }

        if(selected == corbq3)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq3)
        {
            wrongAnsBio++;
        }

    }

    public void clickedBq4(View v){

        int selected = -1;

        switch(rgbq4.getCheckedRadioButtonId())
        {
            case R.id.rbbq4o1:
                selected = 1;
                break;
            case R.id.rbbq4o2:
                selected = 2;
                break;
            case R.id.rbbq4o3:
                selected = 3;
                break;
            case R.id.rbbq4o4:
                selected = 4;
                break;
        }

        if(selected == corbq4)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq4)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq5(View v){

        int selected = -1;

        switch(rgbq5.getCheckedRadioButtonId())
        {
            case R.id.rbbq5o1:
                selected = 1;
                break;
            case R.id.rbbq5o2:
                selected = 2;
                break;
            case R.id.rbbq5o3:
                selected = 3;
                break;
            case R.id.rbbq5o4:
                selected = 4;
                break;
        }

        if(selected == corbq5)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq5)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq6(View v){

        int selected = -1;

        switch(rgbq6.getCheckedRadioButtonId())
        {
            case R.id.rbbq6o1:
                selected = 1;
                break;
            case R.id.rbbq6o2:
                selected = 2;
                break;
            case R.id.rbbq6o3:
                selected = 3;
                break;
            case R.id.rbbq6o4:
                selected = 4;
                break;
        }

        if(selected == corbq6)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq6)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq7(View v){

        int selected = -1;

        switch(rgbq7.getCheckedRadioButtonId())
        {
            case R.id.rbbq7o1:
                selected = 1;
                break;
            case R.id.rbbq7o2:
                selected = 2;
                break;
            case R.id.rbbq7o3:
                selected = 3;
                break;
            case R.id.rbbq7o4:
                selected = 4;
                break;
        }

        if(selected == corbq7)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq7)
        {
            wrongAnsBio++;
        }

    }
    public void clickedBq8(View v){

        int selected = -1;

        switch(rgbq8.getCheckedRadioButtonId())
        {
            case R.id.rbbq8o1:
                selected = 1;
                break;
            case R.id.rbbq8o2:
                selected = 2;
                break;
            case R.id.rbbq8o3:
                selected = 3;
                break;
            case R.id.rbbq8o4:
                selected = 4;
                break;
        }

        if(selected == corbq8)
        {
            correctAnsBio++;
        }
        else if(selected >0 && selected != corbq8)
        {
            wrongAnsBio++;
        }

    }
}

Now the issue is that if the user selects an incorrect option first and then changes it to the correct option, both the pointers of incorrect and correct answers are incremented. I have used the onClick attribute in the XML code for the RadioButtons. Probably, there must be some other attribute to just show the option to get the finally checked option instead of it happening again and again.

Following is the XML code:

<RadioGroup
     android:id="@+id/rgbq1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginLeft="14dp">

     <RadioButton
            android:id="@+id/rbbq1o1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:buttonTint="@color/colorPrimary"
            android:onClick="clickedBq1"
            android:text="Option1"
            android:textColor="@color/colorPrimaryVariant"
            android:textSize="20sp" />

2 Answers2

1

you can add radio button change listener

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);        
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // checkedId is the RadioButton selected
    }
})

or get selected radio button on the next button click. like below

    next.setOnClickListener(new OnClickListener(View v){
        String value =
            ((RadioButton)findViewById(rg.getCheckedRadioButtonId()))
            .getText().toString();
          // here you can compare the selected answer is  correct or not     
     });
0

You must give a variable to check whether you have answered before or not ( either the previous answer is true or false ).

Example of method clickedBq1.

public void clickedBq1(View v){
        int selected = -1;
        boolean statusAnswer = false
        switch(rgbq1.getCheckedRadioButtonId())
        {
            case R.id.rbbq1o1:
                selected = 1;
                break;
            case R.id.rbbq1o2:
                selected = 2;
                break;
            case R.id.rbbq1o3:
                selected = 3;
                break;
            case R.id.rbbq1o4:
                selected = 4;
                break;
        }

        if(statusAnswer == false)
        {
            if(selected == corbq1)
            {
                correctAnsBio++;
            }
            else if(selected >0 && selected != corbq1)
            {
                wrongAnsBio++;
            }
            statusAnswer == true
        }
        else
        {
            if(selected == corbq1)
            {
                correctAnsBio++;
                wrongAnsBio--;
            }
            else if(selected >0 && selected != corbq1)
            {
                wrongAnsBio++;
                correctAnsBio--;
            }
        }
    }
  • How does it change anything? The value of statusAnswer will always be false and hence, only the if part will run always. Please correct me if I am wrong. – lazy__panda Mar 14 '20 at 10:06