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.
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.
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.
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.
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.
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.
28 comments
commentshow can i download the source code for it
ReplyI have'nt published the source code file. Will do if you need it
ReplyCould you update for Android Studio 2016? Currently I'm building
ReplyThe database showme this error when i try to connect it.
ReplyWarning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\quiz\quiz.php on line 13
[]
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
Replyimport org.apache.http.client.HttpClient;
Replyimport org.apache.http.client.methods.HttpPost; generate error Could you do an instance with the new tools?
Very Informative article. Where can I fnd the source code?
ReplySir please help. When I start the quiz . Its says Loading pleasewait... but it will Forced Close
ReplyFirst thing is. The php connection. I test it using xamp.
Replylocalhost/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
Id already declare in manifest. what would be the possible error??
Replysir please help me
Replyandroid {
useLibrary 'org.apache.http.legacy'
}
paste that to build.gradle
Hi!
ReplyHow 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.
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.
Replyhiii! getting error in isNetworkStatusAvialable(). can you give me solution for this
Replywhere is menu_quiz and menu_complete xml file ?
ReplyThe correct answer is shown after submitting each question
ReplyThere is no menu_quiz and menu_complete.xml, only complete.java and activity_complete.xml which is given above
ReplyActually when i run the app the data cant loaded its shown an error unfortunatly the app is not responding
ReplySo Can you Help me thankx :)
ReplyHave you posted the code on Github yet? Would like to download the package and give it a shot. Please help. Thanks.
Replyhello it is showing green color is deprecated..pls help me
ReplyMine too that's my problem in my app. do you already have a solution for this?
Replyi have a error in these lines.if(isNetworkStatusAvialable (getApplicationContext())) {
Replyand the other one is public class QuizActivity extends AppCompatActivity {
and last private class AsyncJsonObject extends AsyncTask
The article is updated with your solution
ReplyWhere can i get to download full source code. Getting error while running.
ReplyI getting problem when the last question will move to the scoring page.
Reply10-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