RunCodes
Programming & Tech

How to Create Quiz Apps in Android using SQLite Database and Android Studio? [With Source Code]

1 3,898

If you guyz are searching for the tutorial that show how to create android based quiz apps using Android Studio, Then you are in the right tutorial. 🙂

Before getting into the topic, let’s understand what is the basic concept, how this is programmatically possible and so on. In general quiz application, user select their answer and click send button to submit their answer to the server (in case of online) or to local database (in case of offline) and then they check their answer by click other button. So, we need to understand programmatically what happen when the user select their answer and click on submit button and what is the operation behind when user click on the check button. Being the programmer you can imagine what operation need to perform to do this using Android.

So, our basic concept is when the user select their answer, their answer need to be inserted into the database and return some value when user click the another button for checking answer. So, for this we are going to create one database in our SQLITE. And inside that database we are going to create three table. you can give them any name but for our convenient we give the name of these three table as submittedanswer, answer and compare.

And inside each table we are going to create two column i.e. ID is common in all there table and second column name is: sa in submittedanswer table, ans in answer table and value in compare table. We can insert correct answer in the answer table either by programming or manually but we do it programmatically we can do it manually but it may be time consuming and tedious but again if you want to do so you can watch the following video.  So when the user click their answer and click submit button, their answer will store in submittedanswer table and at the same time that submitted answer will compare with the previously stored correct answer which is store in answer table and should store either correct or incorrect value in the third table i.e. in compare table. And when the user click the Check Button the compare table value should be display in the message box(Toast). So, This is our Basic concept to do this project. So let’s do it.

So let get into the topic, in this tutorial we are gonna learn about many thing during the completion of this small but interesting tutorial.

Steps:

You can see all the below procedure in following Video:

[sociallocker]

1. Before starting this tutorial, you need to have basic knowledge of android but if you are new to android programming it’s OK. it’s not a big deal you will learn gradually. So, in this first step, you need to have Android Studio installed in your system, i am using android studio of version 2.2.3. If you don’t have, download it from here: https://www.android.com/ and then set up AVD (Android Virtual Device). It may be either android studio built-in AVD or any other third party AVD like genymotion. One thing you need to take care while creating AVD is the AVD should be of api level below 22. This step complete the basic setup require to write our code and run it.

2. Open Android Studio, Click on file>new project. Give your application name whatever you want and then click next, select phone and tablet and then click next and then select empty activity and click next and then click on finish. It will take some time to build your gradle.

3. Goto res>layout>activity_main.xml by click the project field from left pane of your android studio and write the following code inside the RelativeLayout tag. or Drag and Drop one RadioGroup and inside that RadioGroup drag and drop four RadioButton. and then drag two button outside RadioGroup and change the properties of these button. For more Detail watch the video. In this step we complete the user interface part.


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ran.com.quiz.MainActivity">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radio_group">

<RadioButton
android:text="8848"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton"
android:layout_weight="1" />

<RadioButton
android:text="9848"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton3"
android:layout_weight="1" />

<RadioButton
android:text="10848"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton2"
android:layout_weight="1" />

<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="11848"
android:id="@+id/radioButton1" />
</RadioGroup>

<Button
android:text="submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radio_group"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="122dp"
android:id="@+id/btnSubmit" />

<Button
android:text="check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnSubmit"
android:layout_toRightOf="@+id/btnSubmit"
android:layout_toEndOf="@+id/btnSubmit"
android:layout_marginLeft="69dp"
android:layout_marginStart="69dp"
android:id="@+id/btncheck" />

</RelativeLayout>

4. Go to java directory of your project from left pane and expand the top most directory of java and then right click on that directory click New>java class, click OK, give name to that java class as DatabaseHelper and extends it using SQliteOpenHelper or write the following code. In this step we create the database and table inside that database.

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by RanBahadurBK on 5/8/2018.
*/

public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME="quiz.db";
public static final String TABLE_ANS="answer";
public static final String TABLE_SA="submittedanaswer";
public static final String TABLE_COM="compare";

//col for ans
public static final String COL_ANS_1="ID";
public static final String COL_ANS_2="ans";

//col for sa
public static final String COL_SA_1="ID";
public static final String COL_SA_2="sa";

//col for ans
public static final String COL_COM_1="ID";
public static final String COL_COM_2="value";

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+TABLE_ANS+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, ans TEXT)");
db.execSQL("CREATE TABLE "+TABLE_SA+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, sa TEXT)");
db.execSQL("CREATE TABLE "+TABLE_COM+ "(ID INTEGER PRIMARY KEY AUTOINCREMENT, value TEXT)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+TABLE_ANS);
db.execSQL("DROP TABLE IF EXISTS"+TABLE_SA);
db.execSQL("DROP TABLE IF EXISTS"+TABLE_COM);
onCreate(db);

}
}

5. Now go to your MainActivity.java file and write the following piece of code. In this step, we are going to write the code to perform inserting the user answer into the database and display inserted result back to user. So, below is the code in MainActivity.java file.

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
Button btnsubmit, btncheck;
SQLiteOpenHelper openHelper;
SQLiteDatabase db;
Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup =(RadioGroup)findViewById(R.id.radioGroup);
btnsubmit=(Button)findViewById(R.id.btnSubmit);
btncheck=(Button)findViewById(R.id.btnCheck);
openHelper=new DatabaseHelper(this);
btnsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int SelectedId= radioGroup.getCheckedRadioButtonId();
radioButton=(RadioButton)findViewById(SelectedId);
String text = radioButton.getText().toString();
db=openHelper.getWritableDatabase();
db=openHelper.getReadableDatabase();
String qry = "SELECT " +DatabaseHelper.COL_ANS_2+ " FROM " +DatabaseHelper.TABLE_ANS+"";
cursor=db.rawQuery(qry, null);
if(cursor!=null){
if(cursor.getCount()>0){
cursor.moveToNext();
}
}else {
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_ANS_2, "8848");
long i = db.insert(DatabaseHelper.TABLE_ANS, null, contentValues);
}

insertdata(text);
cursor = db.rawQuery("SELECT " +DatabaseHelper.TABLE_ANS+ "."
+DatabaseHelper.COL_ANS_2+ "," +DatabaseHelper.TABLE_SA+ "."
+DatabaseHelper.COL_SA_2+ " FROM " +DatabaseHelper.TABLE_ANS+
" INNER JOIN (SELECT " +DatabaseHelper.COL_SA_2+ " FROM "
+DatabaseHelper.TABLE_SA+ " ORDER BY ID DESC LIMIT 1)"
+DatabaseHelper.TABLE_SA+ " ON " +DatabaseHelper.TABLE_SA+ "."
+DatabaseHelper.COL_SA_2+ "=" +DatabaseHelper.TABLE_ANS+ "."
+DatabaseHelper.COL_ANS_2+"", null);
if(cursor!=null){
if(cursor.getCount()>0){
cursor.moveToNext();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_COM_2, "CORRECT");
long dd = db.insert(DatabaseHelper.TABLE_COM, null, contentValues);
}else{
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_COM_2, "INCORRECT");
long dd = db.insert(DatabaseHelper.TABLE_COM, null, contentValues);
}
}

Toast.makeText(getApplicationContext(), "the answer is submitted successfully and the submitted value is"+radioButton.getText(), Toast.LENGTH_LONG).show();

}
});

btncheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db = openHelper.getReadableDatabase();
String check = "SELECT " +DatabaseHelper.COL_COM_2+ " FROM " +DatabaseHelper.TABLE_COM+ " ORDER BY ID DESC LIMIT 1";
cursor= db.rawQuery(check, null);
if(cursor!=null){
if(cursor.getCount()>0){
cursor.moveToNext();
String vall = cursor.getString(cursor.getColumnIndex("value"));
Toast.makeText(getApplicationContext(), "your submitted answer is "+vall, Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
}
}
}
});
}
public void insertdata(String value){
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.COL_SA_2, value);
long id = db.insert(DatabaseHelper.TABLE_SA, null, contentValues);
}

}

6. Finally run the project by clicking on that green triangle button and select your AVD, to submit your answer click on that submit button and to check your result click on that check button. Like this you can develop your own nice and awesome quiz apps this is just the way i show you guyz how you can initialize to develop your quiz application in Android Studio.

[/sociallocker]

if you like this tutorial, share with your friends and comment any suggestion you want to give to us.

For more watch the video:

 

 

Show Comments (1)