How to create a Simple Quiz Application in android

In this tutorial, we are going to create a simple android quiz application. In this application has four options and features like shuffle questions and shuffle the options etc

The App looks Like

Github: https://github.com/rincemathew/SimpleQuizApp

activity_main.xml

<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:text="Questions"
        android:id="@+id/questionTextView"
        android:textSize="20sp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 1"
        android:textAllCaps="false"
        android:textSize="20dp"
        android:textStyle="bold"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:id="@+id/button1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:textSize="20dp"
        android:textStyle="bold"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:id="@+id/button2"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 3"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:textSize="20dp"
        android:textStyle="bold"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:id="@+id/button3"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 4"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:textSize="20dp"
        android:textStyle="bold"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:id="@+id/button4"/>


</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    QstnArray qstnArray;
    AnswerArray answerArray;
    OptionArray optionArray;

    List<Integer> questionNumber;
    List<String> optArray;

    int i=0;

    TextView questionTextView;

    Button button1;
    Button button2;
    Button button3;
    Button button4;


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

        questionTextView =findViewById(R.id.questionTextView);

        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        button3 = findViewById(R.id.button3);
        button4 = findViewById(R.id.button4);

        qstnArray = new QstnArray();
        answerArray = new AnswerArray();

        questionNumber = new ArrayList<>();

        for (int j = 0; j < qstnArray.getquestionListLength(); j++) {
            questionNumber.add(j);
        }

        //shuffle the question list
        Collections.shuffle(questionNumber);


        setQuestion(i);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (button1.getText().toString().equals(answerArray.getAnswer(questionNumber.get(i)))) {
                    i++;
                    Toast.makeText(getApplicationContext(), "Right Answer", Toast.LENGTH_SHORT).show();
                    setQuestion(i);
                } else {
                    i++;
                    Toast.makeText(getApplicationContext(), "wrong Answer", Toast.LENGTH_SHORT).show();
                    setQuestion(i);

                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (button2.getText().toString().equals(answerArray.getAnswer(questionNumber.get(i)))) {
                    i++;
                    Toast.makeText(getApplicationContext(), "Right Answer", Toast.LENGTH_SHORT).show();
                    setQuestion(i);
                } else {
                    i++;
                    Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT).show();
                    setQuestion(i);
                }
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (button3.getText().toString().equals(answerArray.getAnswer(questionNumber.get(i)))) {
                    i++;
                    Toast.makeText(getApplicationContext(), "Right Answer", Toast.LENGTH_SHORT).show();
                    setQuestion(i);
                } else {
                    i++;
                    Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT).show();
                    setQuestion(i);
                }
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (button4.getText().toString().equals(answerArray.getAnswer(questionNumber.get(i)))) {
                    i++;
                    Toast.makeText(getApplicationContext(), "Right Answer", Toast.LENGTH_SHORT).show();
                    setQuestion(i);
                } else {
                    i++;
                    Toast.makeText(getApplicationContext(), "Wrong Answer", Toast.LENGTH_SHORT).show();
                    setQuestion(i);
                }
            }
        });

    }





    public void setQuestion(int i) {

        try {
            if (i < questionNumber.size()) {
                questionTextView.setText(qstnArray.getQuestion(questionNumber.get(i)));
                optionArray = new OptionArray(questionNumber.get(i));
                optArray = new ArrayList<>(optionArray.getOptionArray());

                //suffle the options list
                Collections.shuffle(optArray);
                button1.setText(optArray.get(0));
                button2.setText(optArray.get(1));
                button3.setText(optArray.get(2));
                button4.setText(optArray.get(3));

            } else {
                button1.setEnabled(false);
                button2.setEnabled(false);
                button3.setEnabled(false);
                button4.setEnabled(false);
                Toast.makeText(getApplicationContext(), "game over", Toast.LENGTH_SHORT).show();

            }
        } catch (Exception e) {

        }


    }
}

QstnArray.java

public class QstnArray {
    private int questionNumber;
    private List<String> questionList=new ArrayList<>();

    public QstnArray()
    {

        questionList.add("how to write 'a' in capital letter");//0
        questionList.add("Rhow to write 'b' in capital letter");//1
        questionList.add("how to write 'c' in capital letter");//2
        questionList.add("how to write 'd' in capital letter");//3
        questionList.add("how to write 'e' in capital letter");//4


    }

    public String getQuestion(int questionNumber)
    {
        this.questionNumber=questionNumber;
        return questionList.get(questionNumber);
    }
    public int getquestionListLength()
    {
        return questionList.size();
    }

}

OptionArray.java

public class OptionArray {

    private int questionNumber;
    private List<String> optionArray=new ArrayList<>();

    public OptionArray(int questionNumber)
    {
        this.questionNumber=questionNumber;

        if (questionNumber==0)
        {
            optionArray.add("A");
            optionArray.add("B");
            optionArray.add("C");
            optionArray.add("D");
        }
        else if (questionNumber==1)
        {
            optionArray.add("A");
            optionArray.add("B");
            optionArray.add("C");
            optionArray.add("D");
        }
        else if (questionNumber==2)
        {
            optionArray.add("A");
            optionArray.add("B");
            optionArray.add("C");
            optionArray.add("D");
        }
        else if (questionNumber==3)
        {
            optionArray.add("A");
            optionArray.add("B");
            optionArray.add("C");
            optionArray.add("D");
        }
        else if (questionNumber==4)
        {
            optionArray.add("C");
            optionArray.add("D");
            optionArray.add("E");
            optionArray.add("F");
        }
    }

    public List<String> getOptionArray() {
        return optionArray;
    }
}

AnswerArray.java

public class AnswerArray {
    private int questionNumber;
    private List<String> answerList=new ArrayList<>();

    public AnswerArray()
    {

        answerList.add("A");//0
        answerList.add("B");//1
        answerList.add("C");//2
        answerList.add("D");//3
        answerList.add("E");//4

    }

    public String getAnswer(int questionNumber)
    {
        this.questionNumber=questionNumber;
        return answerList.get(questionNumber);
    }


}

Keep exploring more tutorial from this site. In case of any queries make a comment. Happy coding!!!!

Add a Comment

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