0% found this document useful (0 votes)
96 views

Practical No: Aim: Create Quiz Application Code: //activity - Main - XML

The document describes code for a quiz application with questions about planets in the solar system. It includes XML layout code with buttons and text views, Java code for a MainActivity class to display questions and handle answers, and a Questions class to store question data. The app displays one question at a time with multiple choice answers, tracks the score, and displays a game over message when an incorrect answer is chosen.

Uploaded by

sagar gulhane
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Practical No: Aim: Create Quiz Application Code: //activity - Main - XML

The document describes code for a quiz application with questions about planets in the solar system. It includes XML layout code with buttons and text views, Java code for a MainActivity class to display questions and handle answers, and a Questions class to store question data. The app displays one question at a time with multiple choice answers, tracks the score, and displays a game over message when an incorrect answer is chosen.

Uploaded by

sagar gulhane
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical No:

Aim : Create Quiz Application

Code :

//activity_main.xml
<?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=".MainActivity">

<Button
android:id="@+id/answer4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:gravity="center"
android:text="Button" />

<Button
android:id="@+id/answer3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/answer4"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Button" />

<Button
android:id="@+id/answer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/answer3"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Button" />

<Button
android:id="@+id/answer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/answer2"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Button" />

<TextView
android:id="@+id/score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:gravity="center"
android:textSize="24dp"
android:text="TextView" />

<TextView
android:id="@+id/question"
android:layout_width="match_parent"
android:layout_height="288dp"
android:layout_below="@+id/score"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:gravity="center"
android:textSize="24dp"
android:text="TextView" />
</RelativeLayout>

//MainActivity.java
package com.deepak.admin.quiz;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {


Button answer1,answer2,answer3,answer4;
TextView score,question;
private Questions mQuestions=new Questions();
private String mAnswer;
private int mScore=0;
private int mQuestionsLength=mQuestions.mQuestions.length;

Random r;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

r=new Random();

answer1=(Button)findViewById(R.id.answer1);
answer2=(Button)findViewById(R.id.answer2);
answer3=(Button)findViewById(R.id.answer3);
answer4=(Button)findViewById(R.id.answer4);
score=(TextView)findViewById(R.id.score);
question=(TextView)findViewById(R.id.question);
score.setText("Score: "+mScore);
updatequestion(r.nextInt(mQuestionsLength));

answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer1.getText()==mAnswer)
{
mScore++;
score.setText("Score: "+mScore);
updatequestion(r.nextInt(mQuestionsLength));
}
else
{
gameOver();
}
}
});

answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer2.getText()==mAnswer)
{
mScore++;
score.setText("Score: "+mScore);
updatequestion(r.nextInt(mQuestionsLength));
}
else
{
gameOver();
}
}
});
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer3.getText()==mAnswer)
{
mScore++;
score.setText("Score: "+mScore);
updatequestion(r.nextInt(mQuestionsLength));
}
else
{
gameOver();
}
}
});
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(answer4.getText()==mAnswer)
{
mScore++;
score.setText("Score: "+mScore);
updatequestion(r.nextInt(mQuestionsLength));
}
else
{
gameOver();
}
}
});

private void updatequestion(int num)


{
question.setText(mQuestions.getQuestion(num));
answer1.setText(mQuestions.getChoice1(num));
answer2.setText(mQuestions.getChoice2(num));
answer3.setText(mQuestions.getChoice3(num));
answer4.setText(mQuestions.getChoice4(num));

mAnswer=mQuestions.getCorrectAnswer(num);

private void gameOver()


{
AlertDialog.Builder alertDialogBuilder =new
AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setMessage("Game Over! Your score is
"+mScore)
.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) {
finish();
}
});
AlertDialog alertDialog=alertDialogBuilder.create();
alertDialog.show();

}
}

//Questions (java class)


package com.deepak.admin.quiz;

public class Questions {


public String mQuestions[]={
"Which is the first planet in solar system?",
"Which is the second planet in solar system?",
"Which is the third planet in solar system?",
"Which is the fourth planet in solar system?",
"Which is the fifth planet in solar system?",
"Which is the sixth planet in solar system?",
"Which is the seventh planet in solar system?",
"Which is the eight planet in solar system?"

};

private String mChoices[][]={


{"Mercury","Venus","Earth","Mars"},
{"Jupiter","Venus","Earth","Mars"},
{"Mercury","Saturn","Earth","Mars"},
{"Mercury","Uranus","Earth","Mars"},
{"Jupiter","Venus","Earth","Mars"},
{"Mercury","Venus","Earth","Saturn"},
{"Mercury","Venus","Uranus","Mars"},
{"Mercury","Neptune","Earth","Mars"},

};
private String
mCorrectAnswers[]={"Mercury","Venus","Earth","Mars","Jupiter","Saturn"
,"Uranus","Neptune"};
public String getQuestion(int a)
{
String question=mQuestions[a];
return question;
}
public String getChoice1(int a)
{
String choice=mChoices[a][0];
return choice;
}
public String getChoice2(int a)
{
String choice=mChoices[a][1];
return choice;
}
public String getChoice3(int a)
{
String choice=mChoices[a][2];
return choice;
}
public String getChoice4(int a)
{
String choice=mChoices[a][3];
return choice;
}
public String getChoice5(int a)
{
String choice=mChoices[a][4];
return choice;
}
public String getChoice6(int a)
{
String choice=mChoices[a][5];
return choice;
}
public String getChoice7(int a)
{
String choice=mChoices[a][6];
return choice;
}
public String getChoice8(int a)
{
String choice=mChoices[a][7];
return choice;
}
public String getCorrectAnswer(int a)
{
String answer=mCorrectAnswers[a];
return answer;
}

Output :

You might also like