Practical No: Aim: Create Quiz Application Code: //activity - Main - XML
Practical No: Aim: Create Quiz Application Code: //activity - Main - XML
Practical No: Aim: Create Quiz Application Code: //activity - Main - XML
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;
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();
}
}
});
mAnswer=mQuestions.getCorrectAnswer(num);
}
}
};
};
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 :