• We just launched and are currently in beta. Join us as we build and grow the community.

Android Simple Quiz App Tutorial with Source Code

Parrot

Security Operation Center Analyst
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
163
Likes
151
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Operating System

Android

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

    Button


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

    Button


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

    Button


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

    Button


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

    TextView
  38. android:

    id=

    "@+id/tv_question"
  39. android:

    layout_width=

    "match_parent"
  40. android:

    layout_height=

    "wrap_content"
  41. android:

    layout_centerHorizontal=

    "true"
  42. android:

    text=

    "Question"
  43. android:

    textSize=

    "25sp"
  44. android:

    paddingTop=

    "20sp"
  45. android:

    gravity=

    "center_horizontal"
  46. android:

    layout_above=

    "@+id/btn_one"
  47. android:

    layout_alignParentTop=

    "true"

    />

  48. </

    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

    String

    answer;
  5. private

    int

    questionLength =

    question.questions

    .length

    ;

  6. 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. 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. System

    .exit

    (

    0

    )

    ;
  53. }
  54. }

    )

    ;
  55. alertDialogBuilder.show

    (

    )

    ;

  56. }

  57. private

    void

    NextQuestion(

    int

    num)

    {
  58. tv_question.setText

    (

    question.getQuestion

    (

    num)

    )

    ;
  59. btn_one.setText

    (

    question.getchoice1

    (

    num)

    )

    ;
  60. btn_two.setText

    (

    question.getchoice2

    (

    num)

    )

    ;
  61. btn_three.setText

    (

    question.getchoice3

    (

    num)

    )

    ;
  62. btn_four.setText

    (

    question.getchoice4

    (

    num)

    )

    ;

  63. answer =

    question.getCorrectAnswer

    (

    num)

    ;
  64. }

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

  1. random =

    new

    Random

    (

    )

    ;


  2. btn_one =

    (

    Button

    )

    findViewById(

    R.id

    .btn_one

    )

    ;
  3. btn_one.setOnClickListener

    (

    this

    )

    ;
  4. btn_two =

    (

    Button

    )

    findViewById(

    R.id

    .btn_two

    )

    ;
  5. btn_two.setOnClickListener

    (

    this

    )

    ;
  6. btn_three =

    (

    Button

    )

    findViewById(

    R.id

    .btn_three

    )

    ;
  7. btn_three.setOnClickListener

    (

    this

    )

    ;
  8. btn_four =

    (

    Button

    )

    findViewById(

    R.id

    .btn_four

    )

    ;
  9. btn_four.setOnClickListener

    (

    this

    )

    ;

  10. tv_question =

    (

    TextView)

    findViewById(

    R.id

    .tv_question

    )

    ;

  11. 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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

327,125

327,133

Top