Making a complete Android Quiz Game

In this post I will be posting the code of making a simple android quiz game complete with splash screen and menus. One thing I want to say to you guys is, if you find my work useful please share it on google plus, facebook or twitter and any other site you wish, as you will be helping me and my site grow and I would really appreciate that. Ok, lets get started. Make a new Android project. You can use Eclispe Adt or Android Studio, I’m using Android Studio.

Designing the Interface

Make these layout files in your android project (res>layout):

  • activity_main.xml
  • activity_result.xml
  • options_menu.xml
  • splash.xml

Open the activity_main.xml file and edit it has following:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/res-auto"
 android:id="@+id/relatively"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#d15400"
 android:orientation="vertical"
 android:weightSum="1" >
 <LinearLayout
 android:id="@+id/linearLayout1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.90"
 android:orientation="vertical"
 android:weightSum="1" >
 <LinearLayout
 android:id="@+id/linearLayout11"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.20"
 android:orientation="vertical"
 android:weightSum="1" >
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:orientation="horizontal" >
 <TextView
 android:id="@+id/score"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_gravity="center"
 android:layout_marginRight="10dp"
 android:layout_weight="1"
 android:gravity="center"
 android:text="Score : 1"
 android:textColor="#ffffff"
 android:textSize="25.0sp"
 android:textStyle="bold" />
 <TextView
 android:id="@+id/timers"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_gravity="center"
 android:layout_marginRight="10dp"
 android:layout_weight="1"
 android:gravity="center"
 android:text="00:00:49"
 android:textColor="#ffffff"
 android:textSize="25.0sp"
 android:textStyle="bold" />
 </LinearLayout>
 </LinearLayout>
 <LinearLayout
 android:id="@+id/linearLayout12"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.60"
 android:orientation="vertical"
 android:weightSum="1" >
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.40" >
 <TextView
 android:id="@+id/txtQuestion"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_gravity="center"
 android:layout_marginTop="5dp"
 android:gravity="center"
 android:text="15*2*1-1"
 android:textColor="#ffffff"
 android:textSize="35.0sp"
 android:textStyle="bold" />
 </LinearLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="0.20"
 android:orientation="vertical"
 android:weightSum="1" >
 <Button
 android:id="@+id/button1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginLeft="80dp"
 android:layout_marginRight="80dp"
 android:layout_marginTop="5dp"
 android:background="#fff"
 android:gravity="center"
 android:text="30"
 android:textColor="#000000"
 android:textSize="25.0sp" />
 <Button
 android:id="@+id/button2"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginLeft="80dp"
 android:layout_marginRight="80dp"
 android:layout_marginTop="5dp"
 android:background="#fff"
 android:gravity="center"
 android:text="29"
 android:textColor="#000000"
 android:textSize="25.0sp" />
 <Button
 android:id="@+id/button3"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginLeft="80dp"
 android:layout_marginRight="80dp"
 android:layout_marginTop="5dp"
 android:background="#fff"
 android:gravity="center"
 android:text="32"
 android:textColor="#000000"
 android:textSize="25.0sp" />
 </LinearLayout>
 </LinearLayout>
 </LinearLayout>
</LinearLayout>

game2 (4)

Update the activity_result.xml file as following :

activity_result.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2a3f4c"
android:orientation="vertical"
android:layout_centerInParent="true"
>
<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:layout_height="wrap_content"
android:text="Large Text"
android:textColor="#ffffff"
android:textSize="20sp"
android:padding="10dp"
android:textStyle="bold" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_marginTop="50dp"
android:padding="20dp"
android:layout_gravity="center"
android:onClick="playagain"
android:text="PLAY AGAIN"
android:textColor="#ffffff"
android:textSize="20sp"/>
</LinearLayout>

game2
Edit the optionsmenu.xml file as following :
optionsmenu.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#0080ff"
 android:orientation="vertical">

 <TextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_gravity="center"
 android:layout_marginTop="50dp"
 android:gravity="center"
 android:inputType="textMultiLine"
 android:text="Choose your Game type"
 android:textColor="#ffffff"
 android:textSize="25.0sp"
 android:textStyle="bold" />

 <Button
 android:id="@+id/btnsimple"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginLeft="80dp"
 android:layout_marginRight="80dp"
 android:layout_marginTop="15dp"
 android:background="#fff"
 android:gravity="center"
 android:text="Simple"
 android:textColor="#000000"
 android:textSize="25.0sp" />

 <Button
 android:id="@+id/btnintermediate"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginLeft="80dp"
 android:layout_marginRight="80dp"
 android:layout_marginTop="5dp"
 android:background="#fff"
 android:gravity="center"
 android:text="Intermediate"
 android:textColor="#000000"
 android:textSize="25.0sp" />

 <Button
 android:id="@+id/btnhard"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginLeft="80dp"
 android:layout_marginRight="80dp"
 android:layout_marginTop="5dp"
 android:background="#fff"
 android:gravity="center"
 android:text="Hard"
 android:textColor="#000000"
 android:textSize="25.0sp" />

 <Button
 android:id="@+id/btntime"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginLeft="80dp"
 android:layout_marginRight="80dp"
 android:layout_marginTop="5dp"
 android:background="#fff"
 android:gravity="center"
 android:text="Time based"
 android:textColor="#000000"
 android:textSize="25.0sp" />
 <Button
 android:id="@+id/btnuntime"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:layout_marginLeft="80dp"
 android:layout_marginRight="80dp"
 android:layout_marginTop="5dp"
 android:background="#fff"
 android:gravity="center"
 android:text="Free Run"
 android:textColor="#000000"
 android:textSize="25.0sp" />
</LinearLayout>

game2 (3)

Make changes in splash.xml file as following :

splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:ads="http://schemas.android.com/apk/res-auto"
 android:id="@+id/relatively"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffff80"
 android:orientation="vertical"
 android:weightSum="1" >
 <TextView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:text="Simple Maths Brainer"
 android:layout_weight="1"
 android:textColor="#000"
 android:textSize="34sp"
 android:textStyle="bold"
 android:gravity="center_horizontal|center_vertical"
 />
</LinearLayout>

game2 (2)

 

These four files will make up the design of the game. In the next page I will be posting the code to make up this entire game.


  • Shane Bell

    Thank You Bro. You kept your word.

  • Anna79

    Hello, HITESH VIKANI. Thank you for an excellent tutorial and website. I was trying to add a new field to database and I got stacked because of the mistake: E/CursorWindow: Failed to read row 0, column 6 from a CursorWindow which has 10 rows, 6 columns. I added the new field in the database in the same way as you did. Where can be an error?

  • AnIsh Shakya

    Hello sir, I tried adding more formulas.. but its showing ArrayList.throwIndexOutOfBoundsException. So from where can I access the arraylist to increase its size ?

  • SS

    Hello sir, great tutorial! I am new to coding and would like to know if there was a way to display the highest scores obtained by the user on another screen.

  • Archisman Ghosh

    Hey I want to implement if the correct option is clicked, the color of the button changes to green and if it is wrong it changes to red. Can you help me with the coding?

  • pat

    When the timer hits 00:00 I can still play the game. How can I make it that when it hits 0 it will go to Result Activity.Thanks!

  • Loha Warpe

    Sir. I have a problem, may I have your email address so that i can reach and communicate you about the problem more easily, well, if ya are ok with that.

  • Jhun Rekcah Anon Gilac

    Hello Sir Hitesh. Nice tutorial, but when it times up, the user can continue the game. And how can I random the questions without repeating the question being given? Lot of Thanks!

  • Sankalp Upadhyay

    12-17 10:57:00.900 3316-3316/? E/SELinux: Function: selinux_android_load_priority [0], There is no sepolicy file

    12-17 10:57:00.900 3316-3316/? E/SELinux: Function: selinux_android_load_priority [1], There is no sepolicy version file

    12-17 10:57:00.900 3316-3316/? E/SELinux: Function: selinux_android_load_priority , loading version is VE=SEPF_GT-I9300_4.3_0016

    12-17 10:57:00.900 3316-3316/? E/SELinux: selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
    12-17 10:57:00.905 3316-3316/? D/dalvikvm: Late-enabling CheckJNI
    12-17 10:57:01.415 3316-3316/com.example.developer15.childquiz I/InstantRun: Instant Run Runtime started. Android package is com.example.developer15.childquiz, real application class is null.
    12-17 10:57:01.460 3316-3316/com.example.developer15.childquiz W/InstantRun: No instant run dex files added to classpath
    12-17 10:57:01.475 3316-3316/com.example.developer15.childquiz E/dalvikvm: Could not find class ‘android.util.ArrayMap’, referenced from method com.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources
    12-17 10:57:01.475 3316-3316/com.example.developer15.childquiz W/dalvikvm: VFY: unable to resolve check-cast 1904 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
    12-17 10:57:01.475 3316-3316/com.example.developer15.childquiz D/dalvikvm: VFY: replacing opcode 0x1f at 0x025e
    12-17 10:57:01.475 3316-3316/com.example.developer15.childquiz E/dalvikvm: Could not find class ‘android.util.ArrayMap’, referenced from method com.android.tools.fd.runtime.MonkeyPatcher.pruneResourceCache
    12-17 10:57:01.475 3316-3316/com.example.developer15.childquiz W/dalvikvm: VFY: unable to resolve const-class 1904 (Landroid/util/ArrayMap;) in Lcom/android/tools/fd/runtime/MonkeyPatcher;
    12-17 10:57:01.475 3316-3316/com.example.developer15.childquiz D/dalvikvm: VFY: replacing opcode 0x1c at 0x0060
    12-17 10:57:01.795 3316-3316/com.example.developer15.childquiz D/libEGL: loaded /system/lib/egl/libEGL_mali.so
    12-17 10:57:01.820 3316-3316/com.example.developer15.childquiz D/libEGL: loaded /system/lib/egl/libGLESv1_CM_mali.so
    12-17 10:57:01.825 3316-3316/com.example.developer15.childquiz D/libEGL: loaded /system/lib/egl/libGLESv2_mali.so
    12-17 10:57:01.950 3316-3316/com.example.developer15.childquiz D/OpenGLRenderer: Enabling debug mode 0

    what haappen in this…..anyone can expalin me…. only main.xml is shown but button couldnt work

  • Sankalp Upadhyay

    this code is not working properly…

  • Pingback: Android Application Development - A list of useful Posts • ParallelCodes;()