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 their answers and checks for the correctness of the answer given by the user. It navigates through the questions using dynamic programming.
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.
The following are the links given for all the drawable files that are used in this project.
The complete code for the activity_main.xml file is given below.
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.
Question.javaIn the strings.xml file, we have to provide the question bank. One can add many questions inside this file.
| HOW WELL DO YOU KNOW SIMRAN? IS \n OUT OF 6 correct incorrect loves Chocolates. Knows Following Skills:\n \t \t You Think Simran Believes In:\n \t You Think Simran Wants To Visit Loves Loyalty. Sleeps LessonCreate() 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.