0

I am trying to do an easy check wether the right answer is given to a multiple choice question by using radio buttons and an if-statement.

I wanna use this as a part of an teaching app, however I am probably misunderstanding the usage of

int radioId = radioGroup.getCheckedRadioButtonId()

and I hope you guys can help me. I am using four radio buttons in an radio group and posted xml and java below.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".IntroQuiz1">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="8dp"
        android:text="Quiz1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="56dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="checkButton"
            android:text="RadioButton1" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="checkButton"
            android:text="RadioButton2" />

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="checkButton"
            android:text="RadioButton3" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="checkButton"
            android:text="RadioButton4" />

    </RadioGroup>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="140dp"
        android:layout_marginEnd="156dp"
        android:text=""
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.906"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

    <Button
        android:id="@+id/button_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginTop="52dp"
        android:text="Check"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

public class IntroQuiz1 extends AppCompatActivity {

    RadioGroup radioGroup;
    RadioButton radioButton;
    TextView textView;

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

        radioGroup = findViewById(R.id.radioGroup);
        textView = findViewById(R.id.textView2);


        Button buttonCheck = findViewById(R.id.button_check);
        buttonCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int radioId = radioGroup.getCheckedRadioButtonId();
                int test = 1;

                radioButton = findViewById(radioId);
                String checkString = (String) radioButton.getText();

                if (checkString == "RadioButton1") {
                    textView.setText("Korrekt");
                } else {
                    textView.setText(checkString);
                }

            }
        });
    }

    public void checkButton(View v) {
        int radioId = radioGroup.getCheckedRadioButtonId();

        radioButton = findViewById(radioId);

        Toast.makeText(this, "Selected Radio Button: " + radioButton.getText(),
                Toast.LENGTH_SHORT).show();

    }
}

However, I don't get "Korrekt" printed with any radio button checked. This is already a workaround. I do not want to compare strings. I would rather use the Id, but as I tried to use radioId to check for the correct answer, radioId seems not to be an integer as I would expect from what I learned so far, is that correct? So, I am a bit confused, could someone help me how to correct this code and tell me what I did wrong?

celdrion
  • 27
  • 8
  • Always compare strings in java using equals() method. In your case this will be "RadioButton1".equals(checkString). Check the question provided by @Guy for more information. – Kiryl Tkach Jul 28 '19 at 13:09
  • Thanks guys, good to know. However, comparing strings was only a workaround which I do not want to use at all in this case – celdrion Jul 28 '19 at 17:32

1 Answers1

2

You should be compare your id selected with others, not string; Like:

 int radioId = radioGroup.getCheckedRadioButtonId();
    switch(radioId ){
        case R.id.radioButton1:
            //your code;
            break;
        case R.id.radioButton2:
            //your code;
            break;
        case R.id.radioButton3:
            //your code;
            break;
    }
  • Thank you very much, that works well! However, I can't use it in case I want to allow more than one correct answer. That was why I wanted to use an if-statement. Why does `if (R.id.radioButton1) {textView.setText("Korrekt");} else { textView.setText("Falsch");}}` not work as well? It gives me "error: incompatible types: int cannot be converted to boolean" – celdrion Jul 28 '19 at 17:40
  • R.id.radioButton is a int, if you want more than one correct, you have to use CheckBox not RadioGroup. – Welbert Moreira Jul 28 '19 at 18:13
  • @celdrion just use a fall-through `switch` statement (so one can have a bunch of ids which match). `R.id.radioButton1` is an `int` and not a `boolean` value - besides this `if` statement has absolutely nothing to do with this answer. – Martin Zeitler Jul 28 '19 at 18:22
  • OK, thank you very much. I will have a look at CheckBoxes! But I still don't get why I don't get an int out of radioId. I tried to print it, but I always get "false". Anyways, you guys did help me a lot! – celdrion Jul 28 '19 at 20:29