XML Code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="#FFFFFF"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="Welcome to Java Quiz"
android:textColor="#5382A1"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView"
android:layout_width="89dp"
android:layout_height="123dp"
android:layout_marginTop="108dp"
android:layout_marginEnd="160dp"
android:layout_marginRight="160dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/java" />
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="1. Java was introduced in 1823?"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="85dp"
android:layout_marginLeft="85dp"
android:text="Yes"
android:textSize="8sp"
app:layout_constraintBaseline_toBaselineOf="@+id/no"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:layout_marginEnd="84dp"
android:layout_marginRight="84dp"
android:text="No"
android:textSize="8sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/question" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java Code
package com.example.javaquizzz;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String[] questions = {"Java is a person?",
"Java was introduced in 1233?", "Java was created using Python?",
"Java has abstract classes?", "Java supports interface?", "Java was created by CodeWithHarry in India?"};
private boolean[] answers = {false, false, false, true, true, false};
private int score = 0;
Button yes;
Button no;
TextView question;
private int index= 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yes = findViewById(R.id.yes);
no = findViewById(R.id.no);
question = findViewById(R.id.question);
question.setText(questions[index]);
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// If the array is not going out of bounds
if (index <= questions.length - 1) {
// If you have given correct answer
if(answers[index]){
score++;
}
// Go to the next question
index++;
if (index <= questions.length - 1) {
question.setText(questions[index]);
}
else{
Toast.makeText(MainActivity.this,
"Your score is: " + score + "/" + questions.length,
Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(MainActivity.this, "Restart the app to play again", Toast.LENGTH_SHORT).show();
}
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// If the array is not going out of bounds
if (index <= questions.length - 1) {
// If you have given correct answer
if(!answers[index]){
score++;
}
// Go to the next question
index++;
if (index <= questions.length - 1) {
question.setText(questions[index]);
}
else{
Toast.makeText(MainActivity.this, "Your score is: " + score + "/" + questions.length, Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(MainActivity.this, "Restart the app to play again", Toast.LENGTH_SHORT).show();
}
}
});
}
}