banner



How To Make Quiz App In Android Studio

Android Simple Quiz App Tutorial with Source Code

Submitted by razormist on Tuesday, January 5, 2021 - 13:24.

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. android:id= "@+id/btn_one"

  9. android:layout_width= "match_parent"

  10. android:layout_height= "wrap_content"

  11. android:text= "Button"

  12. android:layout_centerHorizontal= "true"

  13. android:layout_above= "@+id/btn_two" />

  14. android:id= "@+id/btn_two"

  15. android:layout_width= "match_parent"

  16. android:layout_height= "wrap_content"

  17. android:text= "Button"

  18. android:layout_centerHorizontal= "true"

  19. android:layout_above= "@+id/btn_three" />

  20. android:id= "@+id/btn_three"

  21. android:layout_width= "match_parent"

  22. android:layout_height= "wrap_content"

  23. android:text= "Button"

  24. android:layout_centerHorizontal= "true"

  25. android:layout_above= "@+id/btn_four" />

  26. android:id= "@+id/btn_four"

  27. android:layout_width= "match_parent"

  28. android:layout_height= "wrap_content"

  29. android:text= "Button"

  30. android:layout_centerHorizontal= "true"

  31. android:layout_marginBottom= "30dp"

  32. android:layout_alignParentBottom= "true" />

  33. <TextView

  34. android:id= "@+id/tv_question"

  35. android:layout_width= "match_parent"

  36. android:layout_height= "wrap_content"

  37. android:layout_centerHorizontal= "true"

  38. android:text= "Question"

  39. android:textSize= "25sp"

  40. android:paddingTop= "20sp"

  41. android:gravity= "center_horizontal"

  42. android:layout_above= "@+id/btn_one"

  43. android:layout_alignParentTop= "true" />

  44. </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. <application

  5. android:allowBackup= "true"

  6. android:icon= "@mipmap/ic_launcher"

  7. android:label= "@string/app_name"

  8. android:roundIcon= "@mipmap/ic_launcher_round"

  9. android:supportsRtl= "true"

  10. android:theme= "@style/AppTheme" >

  11. <activity android:name= ".MainActivity"

  12. android:configChanges= "orientation"

  13. android:screenOrientation= "portrait" >

  14. <intent-filter>

  15. <action android:name= "android.intent.action.MAIN" />

  16. <category android:name= "android.intent.category.LAUNCHER" />

  17. </intent-filter>

  18. </activity>

  19. </application>

  20. </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. public String choices[ ] [ ] = {

  7. { "HTML", "CSS", "Vala", "PHP" },

  8. { "Punction Marks", "Back-Slash", "Brackets", "Semi Colon" },

  9. { "Siemens Applications", "Student Applications", "Social Applications", "Commercial Applications" }

  10. } ;

  11. public String correctAnswer[ ] = {

  12. "PHP",

  13. "Brackets",

  14. "Commercial Applications"

  15. } ;

  16. public String getQuestion( int a) {

  17. String question = questions[a] ;

  18. return question;

  19. }

  20. public String getchoice1( int a) {

  21. String choice = choices[a] [ 0 ] ;

  22. return choice;

  23. }

  24. public String getchoice2( int a) {

  25. String choice = choices[a] [ 1 ] ;

  26. return choice;

  27. }

  28. public String getchoice3( int a) {

  29. String choice = choices[a] [ 2 ] ;

  30. return choice;

  31. }

  32. public String getchoice4( int a) {

  33. String choice = choices[a] [ 3 ] ;

  34. return choice;

  35. }

  36. public String getCorrectAnswer( int a) {

  37. String answer = correctAnswer[a] ;

  38. return answer;

  39. }

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. private Question question = new Question( ) ;

  4. private int questionLength = question.questions.length ;

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. break ;

  12. case R.id.btn_two :

  13. if (btn_two.getText ( ) == answer) {

  14. Toast.makeText (MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT ).show ( ) ;

  15. NextQuestion(random.nextInt (questionLength) ) ;

  16. } else {

  17. GameOver( ) ;

  18. }

  19. break ;

  20. case R.id.btn_three :

  21. if (btn_three.getText ( ) == answer) {

  22. Toast.makeText (MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT ).show ( ) ;

  23. NextQuestion(random.nextInt (questionLength) ) ;

  24. } else {

  25. GameOver( ) ;

  26. }

  27. break ;

  28. case R.id.btn_four :

  29. if (btn_four.getText ( ) == answer) {

  30. Toast.makeText (MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT ).show ( ) ;

  31. NextQuestion(random.nextInt (questionLength) ) ;

  32. } else {

  33. GameOver( ) ;

  34. }

  35. break ;

  36. }

  37. }

  38. private void GameOver( ) {

  39. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder (MainActivity.this ) ;

  40. alertDialogBuilder

  41. .setMessage ( "Game Over" )

  42. .setCancelable ( false )

  43. .setPositiveButton ( "New Game", new DialogInterface.OnClickListener ( ) {

  44. @Override

  45. public void onClick(DialogInterface dialog, int which) {

  46. startActivity( new Intent(getApplicationContext( ), MainActivity.class ) ) ;

  47. }

  48. } )

  49. .setNegativeButton ( "Exit", new DialogInterface.OnClickListener ( ) {

  50. @Override

  51. public void onClick(DialogInterface dialog, int which) {

  52. }

  53. } ) ;

  54. alertDialogBuilder.show ( ) ;

  55. }

  56. private void NextQuestion( int num) {

  57. tv_question.setText (question.getQuestion (num) ) ;

  58. btn_one.setText (question.getchoice1 (num) ) ;

  59. btn_two.setText (question.getchoice2 (num) ) ;

  60. btn_three.setText (question.getchoice3 (num) ) ;

  61. btn_four.setText (question.getchoice4 (num) ) ;

  62. answer = question.getCorrectAnswer (num) ;

  63. }

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

              
  1. btn_one = ( Button )findViewById(R.id.btn_one ) ;

  2. btn_one.setOnClickListener ( this ) ;

  3. btn_two = ( Button )findViewById(R.id.btn_two ) ;

  4. btn_two.setOnClickListener ( this ) ;

  5. btn_three = ( Button )findViewById(R.id.btn_three ) ;

  6. btn_three.setOnClickListener ( this ) ;

  7. btn_four = ( Button )findViewById(R.id.btn_four ) ;

  8. btn_four.setOnClickListener ( this ) ;

  9. tv_question = (TextView)findViewById(R.id.tv_question ) ;

  10. 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!!!

  • 27488 views

How To Make Quiz App In Android Studio

Source: https://www.sourcecodester.com/android/12062/android-simple-quiz-app.html

Posted by: burkeawking.blogspot.com

0 Response to "How To Make Quiz App In Android Studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel