Sabtu, 04 Juli 2026

Android - JAVA XML - Quiz 2

 Below is a complete, runnable Android example (Java + XML) for a 5‑question quiz using RadioButtons.

Each question asks: “What is the capital of <country>?”

All RadioButton usage is based on real Android APIs, validated by sources 1 2.

The code includes:

  • activity_main.xml
  • MainActivity.java
  • A simple Question model
  • Input validation (cannot submit without selecting an option)
  • Score result display

XML LAYOUT (activity_main.xml)

Place this in app/res/layout/activity_main.xml

Xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="24dp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tvQuestion" android:text="Question" android:textSize="22sp" android:layout_marginBottom="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- RadioGroup for options --> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/rbOption1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" android:textSize="18sp" /> <RadioButton android:id="@+id/rbOption2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" android:textSize="18sp" /> <RadioButton android:id="@+id/rbOption3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 3" android:textSize="18sp" /> <RadioButton android:id="@+id/rbOption4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 4" android:textSize="18sp" /> </RadioGroup> <Button android:id="@+id/btnNext" android:text="Next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24dp" /> </LinearLayout>

MODEL CLASS (Question.java)

Place in app/java/yourpackage/Question.java

Java
package your.package.name; public class Question { public String questionText; public String[] options; // 4 options public int correctIndex; // 0-3 public Question(String questionText, String[] options, int correctIndex) { this.questionText = questionText; this.options = options; this.correctIndex = correctIndex; } }

MAIN ACTIVITY (MainActivity.java)

Java
package your.package.name; import android.os.Bundle; import android.widget.Button; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private TextView tvQuestion; private RadioGroup radioGroup; private RadioButton rb1, rb2, rb3, rb4; private Button btnNext; private Question[] questions; private int currentIndex = 0; private int score = 0; // count correct answers @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvQuestion = findViewById(R.id.tvQuestion); radioGroup = findViewById(R.id.radioGroup); rb1 = findViewById(R.id.rbOption1); rb2 = findViewById(R.id.rbOption2); rb3 = findViewById(R.id.rbOption3); rb4 = findViewById(R.id.rbOption4); btnNext = findViewById(R.id.btnNext); // 5 country‑capital questions questions = new Question[] { new Question("What is the capital of Japan?", new String[]{"Tokyo", "Beijing", "Seoul", "Bangkok"}, 0), new Question("What is the capital of France?", new String[]{"Madrid", "Paris", "Lisbon", "Rome"}, 1), new Question("What is the capital of Australia?", new String[]{"Sydney", "Melbourne", "Canberra", "Perth"}, 2), new Question("What is the capital of Canada?", new String[]{"Toronto", "Ottawa", "Vancouver", "Montreal"}, 1), new Question("What is the capital of Brazil?", new String[]{"São Paulo", "Brasília", "Rio de Janeiro", "Salvador"}, 1) }; loadQuestion(); btnNext.setOnClickListener(v -> { try { int checkedId = radioGroup.getCheckedRadioButtonId(); if (checkedId == -1) { Toast.makeText(this, "Please select an option", Toast.LENGTH_SHORT).show(); return; } int selectedIndex = getSelectedIndex(checkedId); if (selectedIndex == questions[currentIndex].correctIndex) { score++; } currentIndex++; if (currentIndex < questions.length) { loadQuestion(); } else { Toast.makeText(this, "Quiz Finished! Your Score: " + score + "/5", Toast.LENGTH_LONG).show(); btnNext.setEnabled(false); } } catch (Exception e) { Toast.makeText(this, "An error occurred", Toast.LENGTH_SHORT).show(); } }); } private int getSelectedIndex(int id) { if (id == rb1.getId()) return 0; if (id == rb2.getId()) return 1; if (id == rb3.getId()) return 2; return 3; // rb4 } private void loadQuestion() { radioGroup.clearCheck(); Question q = questions[currentIndex]; tvQuestion.setText(q.questionText); rb1.setText(q.options[0]); rb2.setText(q.options[1]); rb3.setText(q.options[2]); rb4.setText(q.options[3]); } }

NOTES

  • RadioButton + RadioGroup behavior is valid according to Android best practices 1 2.
  • Code fully runnable in any Android Studio Java project.
  • Handles input validation, error handling, and UI updates.

Want enhancements?

I can also provide:

  • Kotlin version
  • Multiple screens / scoring screen
  • Randomized questions
  • Database‑based quiz
  • Custom RadioButton layout

Just tell me!

Android - Java XML - Quiz 1

 Here is a clean, complete template for a multiple-choice quiz using standard Android Java and an XML layout. It features custom styling for the options using a RadioGroup and handles the question state dynamically.

1. The Layout (activity_main.xml)

This UI uses a TextView for the question, a RadioGroup to hold the choices (ensuring only one can be selected at a time), and a Button to submit the answer.

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_width"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="24dp"
    android:gravity="center_vertical">

    <!-- Question Counter -->
    <TextView
        android:id="@+id/tv_question_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Question 1 of 3"
        android:textSize="14sp"
        android:textColor="#757575"
        android:layout_marginBottom="8dp" />

    <!-- The Question -->
    <TextView
        android:id="@+id/tv_question"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="What is the capital city of France?"
        android:textSize="22sp"
        android:textStyle="bold"
        android:textColor="#212121"
        android:layout_marginBottom="24dp" />

    <!-- Options Container -->
    <RadioGroup
        android:id="@+id/rg_options"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp">

        <RadioButton
            android:id="@+id/rb_option1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_marginBottom="8dp"
            android:text="London"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/rb_option2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_marginBottom="8dp"
            android:text="Berlin"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/rb_option3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_marginBottom="8dp"
            android:text="Paris"
            android:textSize="16sp" />

        <RadioButton
            android:id="@+id/rb_option4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp"
            android:layout_marginBottom="8dp"
            android:text="Madrid"
            android:textSize="16sp" />
    </RadioGroup>

    <!-- Action Button -->
    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:text="Submit Answer"
        android:textSize="16sp" />

</LinearLayout>

2. The Logic (MainActivity.java)

This class manages the quiz state, validates whether an option was chosen, tallies the score, and handles advancing to the next question.

Java
package com.example.quizapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvQuestionNumber, tvQuestion;
    private RadioGroup rgOptions;
    private RadioButton rbOption1, rbOption2, rbOption3, rbOption4;
    private Button btnSubmit;

    // Quiz Data Arrays
    private final String[] questions = {
            "What is the capital city of France?",
            "Which planet is known as the Red Planet?",
            "What is the largest mammal on Earth?"
    };

    private final String[][] options = {
            {"London", "Berlin", "Paris", "Madrid"},
            {"Earth", "Mars", "Jupiter", "Saturn"},
            {"Elephant", "Blue Whale", "Giraffe", "Orca"}
    };

    // Index tracking for the correct answers (0-indexed)
    private final int[] correctAnswers = {2, 1, 1}; // Paris, Mars, Blue Whale

    private int currentQuestionIndex = 0;
    private int score = 0;

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

        // Initialize UI Elements
        tvQuestionNumber = findViewById(R.id.tv_question_number);
        tvQuestion = findViewById(R.id.tv_question);
        rgOptions = findViewById(R.id.rg_options);
        rbOption1 = findViewById(R.id.rb_option1);
        rbOption2 = findViewById(R.id.rb_option2);
        rbOption3 = findViewById(R.id.rb_option3);
        rbOption4 = findViewById(R.id.rb_option4);
        btnSubmit = findViewById(R.id.btn_submit);

        // Load the first question
        loadQuestion();

        // Handle button clicks
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectedId = rgOptions.getCheckedRadioButtonId();

                // 1. Validation check: Ensure they picked an option
                if (selectedId == -1) {
                    Toast.makeText(MainActivity.this, "Please select an answer", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 2. Identify which button was clicked
                RadioButton selectedRadioButton = findViewById(selectedId);
                int selectedAnswerIndex = rgOptions.indexOfChild(selectedRadioButton);

                // 3. Check if the answer is correct
                if (selectedAnswerIndex == correctAnswers[currentQuestionIndex]) {
                    score++;
                    Toast.makeText(MainActivity.this, "Correct!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Wrong Answer!", Toast.LENGTH_SHORT).show();
                }

                // 4. Advance or show results
                currentQuestionIndex++;
                if (currentQuestionIndex < questions.length) {
                    loadQuestion();
                } else {
                    showResults();
                }
            }
        });
    }

    private void loadQuestion() {
        // Reset the selection container
        rgOptions.clearCheck();

        // Update Text
        String progressText = "Question " + (currentQuestionIndex + 1) + " of " + questions.length;
        tvQuestionNumber.setText(progressText);
        tvQuestion.setText(questions[currentQuestionIndex]);
        
        // Update Options
        rbOption1.setText(options[currentQuestionIndex][0]);
        rbOption2.setText(options[currentQuestionIndex][1]);
        rbOption3.setText(options[currentQuestionIndex][2]);
        rbOption4.setText(options[currentQuestionIndex][3]);

        // If it's the final question, change button text
        if (currentQuestionIndex == questions.length - 1) {
            btnSubmit.setText("Finish Quiz");
        }
    }

    private void showResults() {
        // Disable choices and update UI to show score
        tvQuestionNumber.setText("Quiz Completed");
        tvQuestion.setText("Your final score is: " + score + " / " + questions.length);
        rgOptions.setVisibility(View.GONE);
        
        // Turn the button into a restart button
        btnSubmit.setText("Restart Quiz");
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Reset state parameters
                currentQuestionIndex = 0;
                score = 0;
                rgOptions.setVisibility(View.VISIBLE);
                btnSubmit.setText("Submit Answer");
                
                // Re-wire original behavior
                recreate(); 
            }
        });
    }
}

How the mechanics work under the hood:

  • rgOptions.clearCheck(): It is critical to clear the previous selection before loading a new question. Otherwise, if a user selected option 3 on question 1, option 3 will remain checked when question 2 displays.

  • rgOptions.indexOfChild(...): This helper maps the active RadioButton directly to its integer index inside the group layout ($0, 1, 2,$ or $3$). This allows for quick math comparisons against the correctAnswers index array without needing to parse the actual string labels.