Introduction
In this lesson, we’ll attempt to use Android to build a simple quiz app. This easy-to-use program may generate arbitrary questions and options for responses. Google created the smartphone operating program called Android. It is utilized in many devices, including televisions, tablets, and smartphones. Android is available as an open source for developers who are interested in creating mobile applications. Additionally, it offers a flexible foundation that makes it easier for the developer to create programs. Because Android is open-source, it is simple for developers to create and add new features. Let’s begin the coding now.
Getting Started:
First of all, you should have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open-source development tool that helps to make things.
for the Android Studio press the download button
Layout Design
Now that the design for the application is complete, find the layout file called activity main.xml, which is the name that appears by default when a new activity is created. After that, add these scripts to your layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.razormist.simplequizapp.MainActivity">
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerHorizontal="true"
android:layout_above="@+id/btn_two"/>
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerHorizontal="true"
android:layout_above="@+id/btn_three"/>
<Button
android:id="@+id/btn_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerHorizontal="true"
android:layout_above="@+id/btn_four"/>
<Button
android:id="@+id/btn_four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"/>
<TextView
android:id="@+id/tv_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Question"
android:textSize="25sp"
android:paddingTop="20sp"
android:gravity="center_horizontal"
android:layout_above="@+id/btn_one"
android:layout_alignParentTop="true"/>
</RelativeLayout>
Android Manifest File
The Android system needs the Android Manifest file before it can run the code since it contains crucial information about your app. It provides an overview of the application’s data. It includes some libraries required to access various app methods.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.razormist.simplequizapp">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Making The Quiz Content
The list of all the quizzes in the quiz app is contained in this code. Simply build a new class in Java called Question and then add these code blocks inside of it to accomplish this.
public String questions[] = {
"Which is a Programming Language?",
"In COMAL language program, after name of procedure parameters must be in?",
"Programming language COBOL works best for use in?"
};
public String choices[][] = {
{"HTML", "CSS", "Vala", "PHP"},
{"Punction Marks", "Back-Slash", "Brackets", "Semi Colon"},
{"Siemens Applications", "Student Applications", "Social Applications", "Commercial Applications"}
};
public String correctAnswer[] = {
"PHP",
"Brackets",
"Commercial Applications"
};
public String getQuestion(int a){
String question = questions[a];
return question;
}
public String getchoice1(int a){
String choice = choices[a][0];
return choice;
}
public String getchoice2(int a){
String choice = choices[a][1];
return choice;
}
public String getchoice3(int a){
String choice = choices[a][2];
return choice;
}
public String getchoice4(int a){
String choice = choices[a][3];
return choice;
}
public String getCorrectAnswer(int a){
String answer = correctAnswer[a];
return answer;
}
The Main Function
The primary purpose of the application is contained in this code. Every time the answer is accurate, this code will automatically generate a new question. Write this variable inside the MainActivity class after finding and opening the MainActivity Java file.
Button btn_one, btn_two, btn_three, btn_four;
TextView tv_question;
private Question question = new Question();
private String answer;
private int questionLength = question.questions.length;
Random random;
The MainActivity class should then implement this handler, which will listen to all button functions within the activity.
implements View.OnClickListener
Then write this method to make to code work correctly to active command.
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_one:
if(btn_one.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
case R.id.btn_two:
if(btn_two.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
case R.id.btn_three:
if(btn_three.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
case R.id.btn_four:
if(btn_four.getText() == answer){
Toast.makeText(MainActivity.this, "You Are Correct", Toast.LENGTH_SHORT).show();
NextQuestion(random.nextInt(questionLength));
}else{
GameOver();
}
break;
}
}
private void GameOver(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setMessage("Game Over")
.setCancelable(false)
.setPositiveButton("New Game", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
})
.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
alertDialogBuilder.show();
}
private void NextQuestion(int num){
tv_question.setText(question.getQuestion(num));
btn_one.setText(question.getchoice1(num));
btn_two.setText(question.getchoice2(num));
btn_three.setText(question.getchoice3(num));
btn_four.setText(question.getchoice4(num));
answer = question.getCorrectAnswer(num);
}
Finally, initialize the required methods inside the onCreate method to run the application.
random = new Random();
btn_one = (Button)findViewById(R.id.btn_one);
btn_one.setOnClickListener(this);
btn_two = (Button)findViewById(R.id.btn_two);
btn_two.setOnClickListener(this);
btn_three = (Button)findViewById(R.id.btn_three);
btn_three.setOnClickListener(this);
btn_four = (Button)findViewById(R.id.btn_four);
btn_four.setOnClickListener(this);
tv_question = (TextView)findViewById(R.id.tv_question);
NextQuestion(random.nextInt(questionLength));
So there you have it—we used Android to develop a simple quiz app. I sincerely hope that this tutorial provides you with the information you need. Please visit this website for further information and tutorials.