Quize App Code

MainActivity.java

package com.example.quiz_app;

import android.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    TextView questionTextView;
    TextView totalQuestionTextView;
    Button ansA,ansB,ansC,ansD;
    Button btn_submit;

    int score=0;
    int totalQuestion = QuestionAnswer.question.length;
    int currentQuestionIndex =0;
    String selectedAnswer="";

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

        totalQuestionTextView = findViewById(R.id.total_question);
        questionTextView = findViewById(R.id.question);
        ansA = findViewById(R.id.ans_a);
        ansB = findViewById(R.id.ans_b);
        ansC= findViewById(R.id.ans_c);
        ansD = findViewById(R.id.ans_d);
        btn_submit = findViewById(R.id.btn_submit);

        ansA.setOnClickListener(this);
        ansB.setOnClickListener(this);
        ansC.setOnClickListener(this);
        ansD.setOnClickListener(this);
        btn_submit.setOnClickListener(this);


        totalQuestionTextView.setText("Total question: "+totalQuestion);

        loadNewQuestion();
    }

    private void loadNewQuestion(){
        if(currentQuestionIndex == totalQuestion){
            finishQuiz();
            return;
        }
        questionTextView.setText(QuestionAnswer.question[currentQuestionIndex]);
        ansA.setText(QuestionAnswer.choices[currentQuestionIndex][0]);
        ansB.setText(QuestionAnswer.choices[currentQuestionIndex][1]);
        ansC.setText(QuestionAnswer.choices[currentQuestionIndex][2]);
        ansD.setText(QuestionAnswer.choices[currentQuestionIndex][3]);

        selectedAnswer="";

    }

    private void finishQuiz(){
        String passStatus;
        if(score >= totalQuestion*0.6){
            passStatus = "Passed";
        }else{
            passStatus = "Failed";
        }
        new AlertDialog.Builder(this)
                .setTitle(passStatus)
                .setMessage("Your Score is "+score+" Out of "+totalQuestion)
                .setPositiveButton("Restart",((dialog, i) -> restartQuiz() ))
                .setCancelable(false)
                .show();
    }

    private void restartQuiz(){
        score = 0;
        currentQuestionIndex=0;
        loadNewQuestion();
    }

    @Override
    public void onClick(View view){
        ansA.setBackgroundColor(Color.WHITE);
        ansB.setBackgroundColor(Color.WHITE);
        ansC.setBackgroundColor(Color.WHITE);
        ansD.setBackgroundColor(Color.WHITE);

        Button clickedButton = (Button) view;

        if(clickedButton.getId() == R.id.btn_submit) {
            if(!selectedAnswer.isEmpty()){
                if(selectedAnswer.equals(QuestionAnswer.correctAnswers[currentQuestionIndex])){
                    score++;
                }else{
                    clickedButton.setBackgroundColor(Color.MAGENTA);
                }
                currentQuestionIndex++;
                loadNewQuestion();
            }else{

            }
        }
        else{
            selectedAnswer=clickedButton.getText().toString();
            clickedButton.setBackgroundColor(Color.YELLOW);
        }
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="#E35B5B"
    android:padding="24dp"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/total_question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Total Question"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textColor="#EEE0F1"
        android:textSize="30dp"
        android:textStyle="bold"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/choices_layout"
        android:layout_centerInParent="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/question"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="150dp"
            android:text="This will be our question"
            android:textColor="#F7F5F8"
            android:textSize="24dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/ans_a"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="#FBFBFB"
            android:text="Ans A"
            android:textColor="@color/black"
            android:textSize="24sp"/>
        <Button
            android:id="@+id/ans_b"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="#FBFBFB"
            android:text="Ans B"
            android:textColor="@color/black"
            android:textSize="24sp"/>
        <Button
            android:id="@+id/ans_c"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="#FBFBFB"
            android:text="Ans C"
            android:textColor="@color/black"
            android:textSize="24sp"/>
        <Button
            android:id="@+id/ans_d"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="#FBFBFB"
            android:text="Ans D"
            android:textColor="@color/black"
            android:textSize="24sp"/>
    </LinearLayout>

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#65E614"
        android:text="Submit"
        android:layout_below="@id/choices_layout"
        android:textColor="@color/black"
        android:textSize="24sp"/>

</RelativeLayout>

QuestionAnswer.java

package com.example.quiz_app;

public class QuestionAnswer {

    public static String question []={
            "What is 10+26 ?",
            "Who invented Telephone?",
            "what is 12*9 ?",
            "who is the founder of SpaceX?",
            "In the given options, which is the Example of System Software?"
    };
    public static String choices [][]={
            {"32" , "42" , "36" , "38"},
            {"Graham Bell" , "Einstein" , "Edison" , "None of the above"},
            {"96" , "84" , "102" , "108"},
            {"Jeff Bezos" , "Elon Musk" , "Steve Jobs" , "Bill Gates"},
            {"Windows" , "Linux" , "MacOS" , "All of the above"}
    };
    public static String correctAnswers []={
            "36" ,
            "Graham Bell",
            "108",
            "Elon Musk",
            "All of the above"};
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *