How to Create a Quiz App In Android?
Android is an operating system which is built basically 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 baby Android apps to learn Android. In this article let’s create a simple Quiz App in android using Java. A simple Quiz App that contains a set of curated questions and its answers and checks for the correctness of the answer given by the user. It navigates through the questions using dynamic programming.
Step 1: Creating a new project
- Click on the File option at the topmost corner in the left.
- Then click on new and open a new project and name the project.
- Now select the Empty Activity with language as Java.
- Name it QuizApp.
Step 2: Designing the UI 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.
Adding images in the drawable folder:
The following are the links given for all the drawable files that are used in this project.
- For flower images (f1/f2/f3/f4/f5/f6/f7): Refer to this.
- For the next icon: Refer to this.
- For the previous icon: Refer to this.
- For emoji: Refer to this.
The complete code for the activity_main.xml file is given below.
After adding this code in activity_main.xml the UI looks like:
Step 3: Working with Question.java
To create a new Java class right-click a Java file or folder, and select New > Java Class.
Now add the following code in the Question.java. Here use getters and setters method to retrieve and set data. Method isAnswerTrue() returns answerTrue as already passed in Question Constructor.
Step 4: Working with strings.xml file
In the strings.xml file, we have to provide the question bank. One can add many questions inside this file.
Step 5: Working with MainActivity.java
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.
Output: Run on Emulator
Complete QUIZ App project in Android –
Please Login to comment...