Hi i am Beginner in Android and Java..I am currently building Android quiz game. The application should display 6 questions with 4 answers, if the player's answer is correct then player will move to another question and score 5 points, feedback to the player should be provided as well (im planning to use Toast for this one). In the end, player will get to the final screen to see the total score and correct answer.. here is what i have done so far:

public class play extends Activity implements OnClickListener {



    private int totalcorrectanswers;
    private int correctanswers;
    private Random random;
    private TextView questiontextview;
    private int score;
    private int finalscore;


    Button answer1,answer2,answer3,answer4;
    Button AnswerButtons [] = {answer1,answer2,answer3,answer4};







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

            View AnswerButton1 = findViewById(R.id.answerbutton1);
            AnswerButton1.setOnClickListener(this);
            View AnswerButton2 = findViewById(R.id.answerbutton2);
            AnswerButton2.setOnClickListener(this);
            View AnswerButton3 = findViewById(R.id.answerbutton3);
            AnswerButton3.setOnClickListener(this);
            View AnswerButton4 = findViewById(R.id.answerbutton4);
            AnswerButton4.setOnClickListener(this);



        }







    public play() {




    }


    @Override
    public void onClick(View v) {





    }

}

and here is the XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity = "center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Score"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/scorenumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/questions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:gravity="center" >

        <Button
            android:id="@+id/answerbutton1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/answerbutton2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/answerbutton3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/answerbutton4"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

</LinearLayout>

Now, I am not sure how to display the 6 Questions and 4 answers by using ArrayList?
i have never done quiz/trivia game before..

Dani AI

Generated

— a simple, maintainable way to build this is to separate three concerns: data (questions), model (Question objects + an ArrayList), and UI (binding one Question at a time to your Views). Store questions as a JSON file in /assets or /res/raw, parse it into an ArrayList<Question>, shuffle and take the first six for the round, then present one question at a time and advance the index when the user answers.

A tiny model example (keep it Parcelable so you can save/restore easily):

public class Question implements Parcelable {
    public String text;
    public String[] choices;
    public int correctIndex;
    // constructor, getters and Parcelable implementation omitted for brevity
}

Example JSON format to put in assets/questions.json:

[
  {"q":"Which planet is red?","choices":["Venus","Mars","Jupiter","Saturn"],"answer":1},
  {"q":"2+2?","choices":["3","4","5","6"],"answer":1}
]

Loading strategy (overview, not full Activity code): read the JSON from assets on a background thread, build an ArrayList<Question>, call Collections.shuffle(list) and then use List<Question> round = new ArrayList<>(list).subList(0, Math.min(6, list.size())); to get six unique questions. Keep current index and score in fields and persist them in onSaveInstanceState (or make the data Parcelable and store it) so rotation won’t reset the game.

Practical tips: disable answer buttons immediately on tap to avoid double-taps; give visible feedback (change button color) and wait a short delay (Handler.postDelayed) before showing the next question; validate that the JSON has at least six entries to avoid IndexOutOfBounds; avoid long file parsing on the UI thread (use an Executor/AsyncTask). For the final screen pass score and a list of missed questions via Intent extras (use Parcelable for speed).

the questions and answers also should be stored in file (im not planning to use SQLdbhelper)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.