Teknologiskolen

Android Studio quiz

 

MainActivity.java:

package teknologiskolen.com.quiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button btnLeft1;
    private Button btnRight1;
    private Button btnLeft2;
    private Button btnRight2;

    private TextView txtQuestion;
    private TextView txtAnswer;

    private int randomQuestion;

    // Det rigtige svar står på pladsen efter spørgsmålet
    String quiz[][] = {
        {"What is the name of the British flag?", "Union Jack", "Stars and Stripes", "Maple Leaf", "St Andrew's Cross"},
        {"What is the name of the American flag?", "Stars and Stripes", "Union Jack", "Maple Leaf", "St Andrew's Cross"}
    };

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

        // Initialiserer tekstfelter
        txtQuestion = (TextView)findViewById(R.id.txtQuestion);
        txtAnswer = (TextView)findViewById(R.id.txtAnswer);

        // Initialiserer knapper
        btnLeft1 = (Button)findViewById(R.id.left1);
        btnRight1 = (Button)findViewById(R.id.right1);
        btnLeft2 = (Button)findViewById(R.id.left2);
        btnRight2 = (Button)findViewById(R.id.right2);

        // Sætter en fælles lytter på knapperne
        btnLeft1.setOnClickListener(this);
        btnRight1.setOnClickListener(this);
        btnLeft2.setOnClickListener(this);
        btnRight2.setOnClickListener(this);

        // Vælger et tilfældigt spørgsmål
        pickRandomQuestion();
    }

    private void pickRandomQuestion(){
        Random random = new Random();
        randomQuestion = random.nextInt(quiz.length);

        // Sæt spørgsmålet på tekstfeltet
        txtQuestion.setText(quiz[randomQuestion][0]);

        List<String> questions = new ArrayList<>();

        // Læg alle svarene i en liste
        for(int i = 1; i < quiz[randomQuestion].length; i++){
            questions.add(quiz[randomQuestion][i]);
        }

        // Bland spørgsmål
        Collections.shuffle(questions);

        // Sæt svarene på knapperne
        btnLeft1.setText(questions.get(0));
        btnRight1.setText(questions.get(1));
        btnLeft2.setText(questions.get(2));
        btnRight2.setText(questions.get(3));
    }

    @Override
    public void onClick(View v) {
        Button button = (Button)v;
        String buttonText = button.getText().toString();

        if(buttonText.equals(quiz[randomQuestion][1])){
            txtAnswer.setText("Correct!");
        }
        else{
            txtAnswer.setText("That was not correct. Try again!");
        }
    }
}

activity_mail.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <TextView
            android:text="Question"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="22dp"
            android:id="@+id/txtQuestion" />
    </FrameLayout>

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:baselineAligned="false">

        <Button
            android:text="left1"
            android:id="@+id/left1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".50" />

        <Button
            android:text="right1"
            android:id="@+id/right1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".50" />

    </LinearLayout>

    <LinearLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:baselineAligned="false">

        <Button
            android:text="left2"
            android:id="@+id/left2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".50" />

        <Button
            android:text="right2"
            android:id="@+id/right2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".50" />
        </LinearLayout>

    <FrameLayout
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <TextView
            android:text="Answer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="16dp"
            android:id="@+id/txtAnswer" />
    </FrameLayout>


</LinearLayout>