Android - Simple Quiz App

Submitted by: 
Operating System: 
Visitors have accessed this post 12973 times.

In this tutorial we will try to create a Simple Quiz App Using Android. This simple application can generate random question and generate choices to be answered. Android is a mobile operating system developed by Google. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provide an adaptive framework that allow the developer to develop an apps in a simpler way. Android is an open source so that developer 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="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simplequizapp.MainActivity">
  11.  
  12. android:id="@+id/btn_one"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="Button"
  16. android:layout_centerHorizontal="true"
  17. android:layout_above="@+id/btn_two"/>
  18.  
  19. android:id="@+id/btn_two"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content"
  22. android:text="Button"
  23. android:layout_centerHorizontal="true"
  24. android:layout_above="@+id/btn_three"/>
  25.  
  26. android:id="@+id/btn_three"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:text="Button"
  30. android:layout_centerHorizontal="true"
  31. android:layout_above="@+id/btn_four"/>
  32.  
  33. android:id="@+id/btn_four"
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:text="Button"
  37. android:layout_centerHorizontal="true"
  38. android:layout_marginBottom="30dp"
  39. android:layout_alignParentBottom="true"/>
  40.  
  41. <TextView
  42. android:id="@+id/tv_question"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:layout_centerHorizontal="true"
  46. android:text="Question"
  47. android:textSize="25sp"
  48. android:paddingTop="20sp"
  49. android:gravity="center_horizontal"
  50. android:layout_above="@+id/btn_one"
  51. android:layout_alignParentTop="true"/>
  52.  
  53. </RelativeLayout>

Android Manifest File

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

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

Creating The Quiz Content

This code contains the list of all questionnaire within the quiz app. To do that simple create a new java file namely Questionnaire 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.

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


Comments

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!

thank you for share

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.

The onCreate method is located in the MainActivity,java. That is where you should put the last part of the code.

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 {
}

Hey!! help to resolve error of getQuestion(num),getchoice1(num)),etc methods written in NextQuestion(int num) function...

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.