Create Android Quiz App with MySQL JSON Parsing and PHP

I have been testing my programming skills recently. So I was trying to study android by creating some simple applications by using Android studio. So, In this post i will share the code for creating an android quiz app. with this app, the user can select one answer from the four options available. The score of the quiz is also displayed at the end of the quiz.

Requirements for Creating Android Quiz app

  • Computer with Android studio Installed
  • Basic Knowledge about Android studio
  • MySQL database (if you dont have a MySQL database, click here to learn more about free MySQL database)

About the Application

First of all. I will tell you what we are trying to achieve to give a basic idea about what we are about to create. In the Home screen you can see a button called start quiz. Now you will be redirected to a screen with a question and four options. user can select one option from the group and click submit. Now it will show the right answer in Green color text. Then, user can click next to go to next question. After completing the quiz, The score is displayed.

android quiz app with scorecreate android free quiz app android studio


Setting up MySQL server for  Android app

First, we have to setup MySQL database for storing questions and answers. Open phpmyadmin and create a database. Inside the database, create a table called quiz_questions with 4 fields. The details of the fields inside the table are given below.

android mysql json php quiz app


Enter some data to the table by using phpmyadmin. ID will be auto incremented. In the possible_answers field, Enter the options seperated by comma. In the correct answer field, enter which is the correct answer. if the third option is the correct answer, Enter the correct_answer field as 3.
Now copy the below code and paste in a notepad file. change the host, user,password and database name and save it as quiz.php .

Now upload this file to the root of your MySQL server by using file manager in your cpanel or by using FTP. Now try accessing the file by using,
http://yoursiteurl.com/quiz.php. If it is showing the data of your table successfully. Save the Url as we need it in future. Proceed with the below steps.

How to create Android quiz app

update (15/09/2017) : The apache http library is depreciated. So you have to add this compile statement in your App level build.gradle file(under dependencies)

  • First of all. create a new project in Android Studio. Enter all the details. Select Blank activity, On next screen click finish
  • In the activity_main.xml, paste the below code.


In the MainActivity.java, Paste the below code


Now create a new activity by right clicking on App and select New>>Activity>>Blank Activity
Name it as QuizActivity. In the activity_quiz.xml, paste below code



In the QuizActivity.java, Paste below code

In line number 185, you can see a space for a url. Paste the url for the MySQL server you created earlier. it will be like http://yoursite.com/quiz.php
Now Create another activitylike above and name it as complete
Paste the below code in activity_complete.xml


Now in the complete.java, Paste the below code


Now, Open strings.xml from res>>values>>strings.xml
Inside resources tag, paste the below code


Thats all. Now try to run the application. Hoorah..  Thats it.
if you face any errors feel free to comment below. I will try to reply as soon as possible.

Share this

Related Posts

Previous
Next Post »

28 comments

comments
May 13, 2016 at 10:00 AM delete This comment has been removed by a blog administrator.
avatar
October 3, 2016 at 11:37 PM delete

how can i download the source code for it

Reply
avatar
October 7, 2016 at 9:57 AM delete

I have'nt published the source code file. Will do if you need it

Reply
avatar
October 20, 2016 at 4:34 AM delete

Could you update for Android Studio 2016? Currently I'm building

Reply
avatar
October 21, 2016 at 2:59 PM delete

The database showme this error when i try to connect it.
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\quiz\quiz.php on line 13
[]

Reply
avatar
October 22, 2016 at 6:35 AM delete

it is an error in your php file. Double check the php file with the one given in the post. Delete the DB and try again with new one

Reply
avatar
October 25, 2016 at 3:02 AM delete

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost; generate error Could you do an instance with the new tools?

Reply
avatar
October 28, 2016 at 3:12 PM delete

Very Informative article. Where can I fnd the source code?

Reply
avatar
November 17, 2016 at 7:23 PM delete

Sir please help. When I start the quiz . Its says Loading pleasewait... but it will Forced Close

Reply
avatar
November 17, 2016 at 7:25 PM delete

First thing is. The php connection. I test it using xamp.
localhost/quiz.php

then it doesnt show the content of my database.

but


Fatal error: Uncaught Error: Call to undefined function mysql_connect() in D:\xampp\htdocs\quiz2.php:7 Stack trace: #0 {main} thrown in D:\xampp\htdocs\quiz2.php on line 7

Reply
avatar
November 17, 2016 at 7:28 PM delete

Id already declare in manifest. what would be the possible error??
sir please help me

Reply
avatar
November 17, 2016 at 7:31 PM delete


android {
useLibrary 'org.apache.http.legacy'
}
paste that to build.gradle

Reply
avatar
November 21, 2016 at 6:38 AM delete

Hi!
How can I build one activity where I can show the result of the test, where I can see which is the correct and incorrect answer.

Reply
avatar
November 22, 2016 at 11:56 PM delete

And other question. Could you show how to do connetion with database in Android Studio 2016? 'cause HTTPCliente and the other libraries are not suportted in A. Studio 2016.

Reply
avatar
December 14, 2016 at 11:42 PM delete

hiii! getting error in isNetworkStatusAvialable(). can you give me solution for this

Reply
avatar
December 16, 2016 at 6:54 AM delete This comment has been removed by the author.
avatar
December 16, 2016 at 8:52 AM delete

where is menu_quiz and menu_complete xml file ?

Reply
avatar
December 18, 2016 at 10:00 AM delete

The correct answer is shown after submitting each question

Reply
avatar
December 18, 2016 at 10:03 AM delete

There is no menu_quiz and menu_complete.xml, only complete.java and activity_complete.xml which is given above

Reply
avatar
December 19, 2016 at 5:43 AM delete

Actually when i run the app the data cant loaded its shown an error unfortunatly the app is not responding

Reply
avatar
December 26, 2016 at 9:46 PM delete

Have you posted the code on Github yet? Would like to download the package and give it a shot. Please help. Thanks.

Reply
avatar
February 5, 2017 at 8:23 AM delete

hello it is showing green color is deprecated..pls help me

Reply
avatar
March 27, 2017 at 5:26 AM delete

Mine too that's my problem in my app. do you already have a solution for this?

Reply
avatar
March 27, 2017 at 9:39 AM delete

i have a error in these lines.if(isNetworkStatusAvialable (getApplicationContext())) {
and the other one is public class QuizActivity extends AppCompatActivity {
and last private class AsyncJsonObject extends AsyncTask

Reply
avatar
September 15, 2017 at 2:26 AM delete

The article is updated with your solution

Reply
avatar
September 24, 2017 at 4:18 AM delete

Where can i get to download full source code. Getting error while running.

Reply
avatar
October 28, 2017 at 2:42 AM delete

I getting problem when the last question will move to the scoring page.

10-28 01:02:25.816 31874-31874/com.example.nuvo.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.nuvo.myapplication, PID: 31874 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nuvo.myapplication/com.example.nuvo.myapplication.Complete}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) at android.app.ActivityThread.access$800(ActivityThread.java:166) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.nuvo.myapplication.Complete.onCreate(Complete.java:18) at android.app.Activity.performCreate(Activity.java:5442) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) at android.app.ActivityThread.access$800(ActivityThread.java:166) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5584) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) at dalvik.system.NativeStart.main(Native Method)

please help me

Reply
avatar