Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java
  • Android
  • Kotlin
  • Flutter
  • Dart
  • Android with Java
  • Android Studio
  • Android Projects
  • Android Interview Questions
Open In App
Next Article:
How to Create a Quiz App In Android?
Next article icon

How to Create a Quiz App In Android?

Last Updated : 31 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Android is an operating system which is basically made for Mobile phones. It is based on the Linux Kernel and other open-source software and is developed by Google. Android is very popular nowadays among students and students are now choosing Android for their projects. It's very much important for a beginner to build basic Android apps to learn Android Development. In this article let's create a simple Quiz App in Android using Java and Kotlin. A simple Quiz App that contains a set of curated questions and their answers and checks for the score at the end.

Step by Step Implementation

Step 1: Creating a new project

To create a new project in the Android Studio, please refer to How to Create/Start a New Project in Android Studio?

Step 2: Working with activity_main.xml

Add the below code in the activity_main.xml file. Here the parent layout is a LinearLayout whose orientation is set to vertical. Inside it, there is one ImageView, one TextView, two Buttons, and two ImageButton. The Button and ImageButton are inside a child LinearLayout for horizontal orientation. ImageView is used for displaying image and TextView is used to display the question and Button is used to indicate true/false and ImageButton for navigating to next/previous question.

activity_main.xml:

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <!--Using linear layout with vertical orientation and center gravity --> <LinearLayout 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:background="#FFFFFF"     android:layout_height="match_parent"     android:orientation="vertical"     android:gravity="center"     tools:context=".MainActivity">      <!--ImageView used for showing pictures along with questions-->     <ImageView         android:id="@+id/myimage"         android:layout_width="wrap_content"         android:src="@drawable/f1"         android:layout_height="wrap_content"/>      <!--TextView used for showing questions on screen-->     <TextView         android:id="@+id/answer_text_view"         android:text="@string/a"         android:textColor="@android:color/black"         android:textSize="30sp"         android:padding="10dp"         android:layout_width="wrap_content"         android:layout_height="wrap_content"/>      <!--Using another LinearLayout for showing buttons         in horizontal orientation-->     <LinearLayout         android:layout_width="wrap_content"         android:layout_height="wrap_content">          <!--TrueButton-->         <Button             android:id="@+id/true_button"             android:layout_marginRight="20dp"             android:backgroundTint="#5BD91B"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:textSize="20sp"             android:text="@string/true_text" />          <!--FalseButton-->         <Button             android:id="@+id/false_button"             android:layout_marginLeft="20dp"             android:layout_width="wrap_content"             android:backgroundTint="#E33328"             android:layout_height="wrap_content"             android:textSize="20sp"             android:text="@string/false_text" />      </LinearLayout>      <LinearLayout         android:layout_width="wrap_content"         android:layout_height="wrap_content">          <!--PreviousButton-->         <ImageButton             android:id="@+id/prev_button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:src="@drawable/baseline_keyboard_arrow_left_black_18dp"             android:backgroundTint="#DFD2D1"             android:text="@string/prev_text" />          <!--NextButton-->         <ImageButton             android:id="@+id/next_button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:backgroundTint="#DFD2D1"             android:src="@drawable/baseline_keyboard_arrow_right_black_18dp"             android:text="@string/next_text" />      </LinearLayout> </LinearLayout> 


Design UI:

quiz-app-ui


Step 3: Working with strings.xml

Navigate to app > res > values > strings.xml and make the following changes. We will be adding 6 questions and fetch them later in MainActivity file.

strings.xml:

XML
<resources>     <string name="app_name">Demo</string>      <string name="a">New Delhi is the capital of India</string>     <string name="b">West Bengal is located in the west of India</string>     <string name="c">Arunachal Pradesh is a state of India</string>     <string name="d">Brazil is located in North America</string>     <string name="e">HTML is a programming language</string>     <string name="f">React is a web development framework</string>  </resources> 


Step 4: Create a data class for questions

To create a new data class right-click a Java/Kotlin file or folder, and select New > Java/Kotlin Class. Now add the following code in the file.

Question.java
package org.geeksforgeeks.demo;  public class Question {     // Resource ID for the question text (stored in strings.xml)     private int answerResId;     // Correct answer (true or false)     private boolean isAnswerTrue;      public Question(int answerResId, boolean isAnswerTrue) {         this.answerResId = answerResId;         this.isAnswerTrue = isAnswerTrue;     }      // Getter for answerResId     public int getAnswerResId() {         return answerResId;     }      // Setter for answerResId     public void setAnswerResId(int answerResId) {         this.answerResId = answerResId;     }      // Getter for isAnswerTrue     public boolean isAnswerTrue() {         return isAnswerTrue;     }      // Setter for isAnswerTrue     public void setAnswerTrue(boolean answerTrue) {         isAnswerTrue = answerTrue;     } } 
Question.kt
package org.geeksforgeeks.demo  data class Question (     var answerResId: Int,     var isAnswerTrue: Boolean ) 


Step 5: Working with MainActivity file

onCreate() method is invoked first when the app is launched. Question[] array is instantiated with question Id and right answer to the question. setOnClickListener() method is invoked whenever Button/ImageButton is clicked, so when the user clicks a button it checks for its Id by getId() method and performs actions as per our logic. updateQuestion() updates question by settext() method of TextView and changes images by keeping track of question number. checkAnswer() method checks the original answer with the button clicked and uses Toast to display text accordingly.

MainActivity.java
package org.geeksforgeeks.demo;  import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {     // UI components     private Button falseButton;     private Button trueButton;     private ImageButton nextButton;     private ImageButton prevButton;     private TextView questionTextView;     private TextView answerTextView;      // Variable to track correct answers     private int correct = 0;      // Index to track the current question     private int currentQuestionIndex = 0;      // Array holding the questions and their correct answers     private final Question[] questionBank = {             new Question(R.string.a, true),             new Question(R.string.b, false),             new Question(R.string.c, true),             new Question(R.string.d, false),             new Question(R.string.e, false),             new Question(R.string.f, true)     };      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          // Initializing UI elements         falseButton = findViewById(R.id.false_button);         trueButton = findViewById(R.id.true_button);         nextButton = findViewById(R.id.next_button);         prevButton = findViewById(R.id.prev_button);         questionTextView = findViewById(R.id.question);         answerTextView = findViewById(R.id.answer);          // Hide the answer text initially         answerTextView.setVisibility(View.INVISIBLE);          // Load the first question         updateQuestion();          // Button click listeners         falseButton.setOnClickListener(v -> checkAnswer(false));         trueButton.setOnClickListener(v -> checkAnswer(true));          nextButton.setOnClickListener(v -> {             answerTextView.setVisibility(View.INVISIBLE);              // Check if there are more questions             if (currentQuestionIndex < 7) {                 currentQuestionIndex++;                  // If all questions are completed, display the score                 if (currentQuestionIndex == 6) {                     nextButton.setVisibility(View.GONE);                     prevButton.setVisibility(View.GONE);                     trueButton.setVisibility(View.GONE);                     falseButton.setVisibility(View.GONE);                      questionTextView.setText("Your Score: " + correct + "/6");                 } else {                     updateQuestion();                 }             }         });          prevButton.setOnClickListener(v -> {             answerTextView.setVisibility(View.INVISIBLE);              // Prevent going back before the first question             if (currentQuestionIndex > 0) {                 currentQuestionIndex = (currentQuestionIndex - 1) % questionBank.length;                 updateQuestion();             }         });     }      // Updates the displayed question     private void updateQuestion() {         questionTextView.setText(questionBank[currentQuestionIndex].getAnswerResId());     }      // Checks the user's answer and updates the UI     private void checkAnswer(boolean userChooseCorrect) {         boolean answerIsTrue = questionBank[currentQuestionIndex].isAnswerTrue();         String message;          if (userChooseCorrect == answerIsTrue) {             message = "That's correct";             correct++;         } else {             message = "That's incorrect";         }          // Display feedback message         answerTextView.setVisibility(View.VISIBLE);         answerTextView.setText(message);     } } 
MainActivity.kt
package org.geeksforgeeks.demo  import android.os.Bundle import android.view.View import android.widget.Button import android.widget.ImageButton import android.widget.TextView import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat  class MainActivity : AppCompatActivity() {          // UI components     private lateinit var falseButton: Button     private lateinit var trueButton: Button     private lateinit var nextButton: ImageButton     private lateinit var prevButton: ImageButton     private lateinit var questionTextView: TextView     private lateinit var answerTextView: TextView      // Variable to track the correct answers     private var correct = 0      // Index to track the current question     private var currentQuestionIndex = 0      // Array holding the questions and their correct answers     private val questionBank: Array<Question> =         arrayOf(             Question(R.string.a, true),             Question(R.string.b, false),             Question(R.string.c, true),             Question(R.string.d, false),             Question(R.string.e, false),             Question(R.string.f, true),         )      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         enableEdgeToEdge()         setContentView(R.layout.activity_main)          // Adjust layout to fit system UI elements         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->             val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)             insets         }          // Initializing UI elements         falseButton = findViewById(R.id.false_button)         trueButton = findViewById(R.id.true_button)         nextButton = findViewById(R.id.next_button)         prevButton = findViewById(R.id.prev_button)         questionTextView = findViewById(R.id.question)         answerTextView = findViewById(R.id.answer)          // Hide the answer text initially         answerTextView.visibility = View.INVISIBLE          // Load the first question         updateQuestion()          // Button click listeners         falseButton.setOnClickListener {             checkAnswer(false)         }         trueButton.setOnClickListener {             checkAnswer(true)         }         nextButton.setOnClickListener {             answerTextView.visibility = View.INVISIBLE              // Check if there are more questions             if (currentQuestionIndex < 7) {                 currentQuestionIndex += 1                  // If all questions are completed, display the score                 if (currentQuestionIndex == 6) {                     nextButton.visibility = View.GONE                     prevButton.visibility = View.GONE                     trueButton.visibility = View.GONE                     falseButton.visibility = View.GONE                      questionTextView.text = "Your Score: $correct/6"                 } else {                     updateQuestion()                 }             }         }         prevButton.setOnClickListener {             answerTextView.visibility = View.INVISIBLE              // Prevent going back before the first question             if (currentQuestionIndex > 0) {                 currentQuestionIndex = ((currentQuestionIndex - 1) % questionBank.size)                 updateQuestion()             }         }     }      // Updates the displayed question     private fun updateQuestion() {         questionTextView.setText(questionBank[currentQuestionIndex].answerResId)     }      // Checks the user's answer and updates the UI     private fun checkAnswer(userChooseCorrect: Boolean) {         val answerIsTrue = questionBank[currentQuestionIndex].isAnswerTrue         val message: String          if (userChooseCorrect == answerIsTrue) {             message = "That's correct"             correct++         } else {             message = "That's incorrect"         }          // Display feedback message         answerTextView.visibility = View.VISIBLE         answerTextView.text = message     } } 


Output:


Next Article
How to Create a Quiz App In Android?

S

simrankak27
Improve
Article Tags :
  • Java
  • Java Quiz
  • Android
  • Kotlin Android
  • Android Projects
  • Java-Android
Practice Tags :
  • Java

Similar Reads

    How to Create a Dice Roller App in Android?
    A dice roller application is a simple application that generates a random number between 1 and a specified maximum number, simulating the roll of a dice. The application is typically used by gamers or anyone who needs to roll a die but doesn't have physical dice available. To create the app, you nee
    2 min read
    How to Create a Social Media App on Android Studio?
    Social media is not a new term for us. Our daily life is incomplete, or we can say we human beings survive on food, water, air, and social media. We are dependent to such an extent that we tend to share every bit of information about ourselves on social media platforms. Similarly, Android Studio is
    8 min read
    How to Create a Voting Application in Android?
    In general, voting means comparing two or more entities on the basis of certain conditions. In this article, we will create a simple voting application that uses two different programming languages namely Java and Python, and ask the user to vote for their preferred language. A sample GIF is given b
    3 min read
    How to add Rate the App feature in Android
    When you publish your app on google play store it is important to get feedback from the user. Unless the user does not love or hate your app, they are not likely to go out of their way to rate your app. Since high rating indicates the success of your app, and even criticism is required to make the a
    2 min read
    How to Create a Basic Intro Slider of an Android App?
    When we download any app and use that app for the very first time. Then we will get to see the intro slider inside our app. With the help of this slider, we educate our users on how they can use that app and it tells in detail about the app. In this article, we will take a look at the implementation
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences