Android Simple Quiz App Tutorial with Source Code

Operating System

In this tutorial, we will try to create a Simple Quiz App Using Android. This simple application can generate random questions and generate choices to be answered. Android is a mobile operating system developed by Google. It used in several gadgets like smartphones, tablets, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provides an adaptive framework that allows the developer to develop apps in a simpler way. Android is open-source so that developers find it easy to develop and expand new features. So let's do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open-source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplequizapp.MainActivity">
  8.  
  9. android:id="@+id/btn_one"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="Button"
  13. android:layout_centerHorizontal="true"
  14. android:layout_above="@+id/btn_two"/>
  15.  
  16. android:id="@+id/btn_two"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:text="Button"
  20. android:layout_centerHorizontal="true"
  21. android:layout_above="@+id/btn_three"/>
  22.  
  23. android:id="@+id/btn_three"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:text="Button"
  27. android:layout_centerHorizontal="true"
  28. android:layout_above="@+id/btn_four"/>
  29.  
  30. android:id="@+id/btn_four"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:text="Button"
  34. android:layout_centerHorizontal="true"
  35. android:layout_marginBottom="30dp"
  36. android:layout_alignParentBottom="true"/>
  37.  
  38. <TextView
  39. android:id="@+id/tv_question"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content"
  42. android:layout_centerHorizontal="true"
  43. android:text="Question"
  44. android:textSize="25sp"
  45. android:paddingTop="20sp"
  46. android:gravity="center_horizontal"
  47. android:layout_above="@+id/btn_one"
  48. android:layout_alignParentTop="true"/>
  49.  
  50. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must require before running the code. It describes the overall information about the application. It contains some libraries that needed to access several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simplequizapp">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity"
  13. android:configChanges="orientation"
  14. android:screenOrientation="portrait">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17.  
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </manifest>

Creating The Quiz Content

This code contains the list of all questionnaires within the quiz app. To do that simply create a new java file namely Question then write these blocks of code inside the newly created java file class.

  1. public String questions[] = {
  2. "Which is a Programming Language?",
  3. "In COMAL language program, after name of procedure parameters must be in?",
  4. "Programming language COBOL works best for use in?"
  5. };
  6.  
  7. public String choices[][] = {
  8. {"HTML", "CSS", "Vala", "PHP"},
  9. {"Punction Marks", "Back-Slash", "Brackets", "Semi Colon"},
  10. {"Siemens Applications", "Student Applications", "Social Applications", "Commercial Applications"}
  11. };
  12.  
  13. public String correctAnswer[] = {
  14. "PHP",
  15. "Brackets",
  16. "Commercial Applications"
  17. };
  18.  
  19. public String getQuestion(int a){
  20. String question = questions[a];
  21. return question;
  22. }
  23.  
  24. public String getchoice1(int a){
  25. String choice = choices[a][0];
  26. return choice;
  27. }
  28.  
  29. public String getchoice2(int a){
  30. String choice = choices[a][1];
  31. return choice;
  32. }
  33.  
  34. public String getchoice3(int a){
  35. String choice = choices[a][2];
  36. return choice;
  37. }
  38.  
  39. public String getchoice4(int a){
  40. String choice = choices[a][3];
  41. return choice;
  42. }
  43.  
  44. public String getCorrectAnswer(int a){
  45. String answer = correctAnswer[a];
  46. return answer;
  47. }

The Main Function

This code contains the main function of the application. This code will automatically generate a random question every time the answer is correct. To start with first locate your MainActivity java file and open it, then write this variable inside the MainActivity class.

  1. Button btn_one, btn_two, btn_three, btn_four;
  2. TextView tv_question;
  3.  
  4. private Question question = new Question();
  5.  
  6. private String answer;
  7. private int questionLength = question.questions.length;
  8.  
  9. Random random;

Then add this handler as implement to the MainActivity class, this will listened to all button function within the activity

  1. implements View.OnClickListener

Then write these method to make to code work correctly.

  1. @Override
  2. public void onClick(View v) {
  3. switch (v.getId()){
  4. case R.id.btn_one:
  5. if(btn_one.getText() == answer){
  6. Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
  7. NextQuestion(random.nextInt(questionLength));
  8. }else{
  9. GameOver();
  10. }
  11.  
  12. break;
  13.  
  14. case R.id.btn_two:
  15. if(btn_two.getText() == answer){
  16. Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
  17. NextQuestion(random.nextInt(questionLength));
  18. }else{
  19. GameOver();
  20. }
  21.  
  22. break;
  23.  
  24. case R.id.btn_three:
  25. if(btn_three.getText() == answer){
  26. Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
  27. NextQuestion(random.nextInt(questionLength));
  28. }else{
  29. GameOver();
  30. }
  31.  
  32. break;
  33.  
  34. case R.id.btn_four:
  35. if(btn_four.getText() == answer){
  36. Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
  37. NextQuestion(random.nextInt(questionLength));
  38. }else{
  39. GameOver();
  40. }
  41.  
  42. break;
  43. }
  44. }
  45.  
  46. private void GameOver(){
  47. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
  48. alertDialogBuilder
  49. .setMessage("Game Over")
  50. .setCancelable(false)
  51. .setPositiveButton("New Game", new DialogInterface.OnClickListener() {
  52. @Override
  53. public void onClick(DialogInterface dialog, int which) {
  54. startActivity(new Intent(getApplicationContext(), MainActivity.class));
  55. }
  56. })
  57. .setNegativeButton("Exit", new DialogInterface.OnClickListener() {
  58. @Override
  59. public void onClick(DialogInterface dialog, int which) {
  60. System.exit(0);
  61. }
  62. });
  63. alertDialogBuilder.show();
  64.  
  65. }
  66.  
  67. private void NextQuestion(int num){
  68. tv_question.setText(question.getQuestion(num));
  69. btn_one.setText(question.getchoice1(num));
  70. btn_two.setText(question.getchoice2(num));
  71. btn_three.setText(question.getchoice3(num));
  72. btn_four.setText(question.getchoice4(num));
  73.  
  74. answer = question.getCorrectAnswer(num);
  75. }

Finally, initialize the require methods inside the onCreate method to run the application.

  1. random = new Random();
  2.  
  3.  
  4. btn_one = (Button)findViewById(R.id.btn_one);
  5. btn_one.setOnClickListener(this);
  6. btn_two = (Button)findViewById(R.id.btn_two);
  7. btn_two.setOnClickListener(this);
  8. btn_three = (Button)findViewById(R.id.btn_three);
  9. btn_three.setOnClickListener(this);
  10. btn_four = (Button)findViewById(R.id.btn_four);
  11. btn_four.setOnClickListener(this);
  12.  
  13. tv_question = (TextView)findViewById(R.id.tv_question);
  14.  
  15. NextQuestion(random.nextInt(questionLength));

Try to run the app and see if it worked.

DEMO

There you have it we have created a Simple Quiz App using Android. I hope that this tutorial helps you with what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Submitted byarpitwon Mon, 03/12/2018 - 06:06

Hello buddy. I tried installing uc mini from http://ucminiapp.in/ Can I edit few features if we got source code clone? if so then how? thanks!
Submitted byJohnny_I (not verified)on Sat, 09/08/2018 - 00:41

The last part of the code. Where to add it. I haven't seen a onCreate method earlier in the code. Am I doing something wrong? Thnaks.
Submitted byRJ1996 (not verified)on Sun, 09/30/2018 - 02:34

Can Someone help me in this part "Then add this handler as implement to the MainActivity class, this will listened to all button function within the activity"

The part that you mean is that you need to add the implements View.OnClickListener in the MainActivity Class to be able to call the buttons via methods. This is where you put the implements. public class MainActivity extends AppCompatActivity implements View.OnClickListener { }
Submitted byTanvi (not verified)on Wed, 10/03/2018 - 02:47

Hey!! help to resolve error of getQuestion(num),getchoice1(num)),etc methods written in NextQuestion(int num) function...
Submitted bysimple and nic… (not verified)on Fri, 03/01/2019 - 19:14

Hello, i applied this code it is very simple and sufficient for any type of quiz process. After initialization one can modify it as per his/her requirement by easy way. Thanks for awesome code !!
Submitted bySanjanaviji (not verified)on Wed, 10/09/2019 - 14:04

Will you please help me .How to use database in the same application?

Submitted bypallabi Guha (not verified)on Fri, 10/18/2019 - 16:31

hi i have an error in the building process

C:\Users\acer\AndroidStudioProjects\SimpleQuizApp\app\src\main\res\layout\activity_main.xml:2: AAPT: error: attribute context (aka com.razormist.simplequizapp:context) not found.

please help

Submitted byMD hafej munshi (not verified)on Wed, 01/27/2021 - 11:07

thank you for share

Add new comment