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
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- How to Publish Your Android App on Amazon App Store for Free?
- Android | Running your first Android app
- How to Add Audio Files to Android App in Android Studio?
- How to create a Facebook login using an Android App?
- How to create a Stopwatch App using Android Studio
- How to create a COVID-19 Tracker Android App
- How to create Swipe Navigation in an Android App
- How to create a Face Detection Android App using Machine Learning KIT on Firebase
- How to Create Static Shortcuts in Android App?
- How to Create a Morse Code Converter Android App?
- How to Create a COVID-19 Tracker Android App to See Details of any City and State in India?
- How to Create a Basic Widget of an Android App?
- How to Create a Dynamic Widget of an Android App?
- How to Create a Dark Mode for a Custom Android App in Kotlin?
- Android | How to Create/Start a New Project in Android Studio?
- How to make a phone call from your Android App?
- Why Kotlin will replace Java for Android App Development
- App Development for Android in 2017: Challenges and Solutions
- Android | Creating a Calendar View app
- Adding Firebase to Android App
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.