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>
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>
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>
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>
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.
Pingback: Android Application Development - A list of useful Posts • ParallelCodes;
Pingback: Project 2 Milestone2 – Mei's Blog