Android studio 7. Develop simple quiz app


Just follow the foolowing link for developing a quiz app

https://www.youtube.com/watch?v=JA9s_Fntg_4&t=139s


The codes corresponding to above video is given below.

MainActivity.java

*****************************************************************************************************************************************
package com.example.hp_pc.hpluniv;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    Button answer1, answer2, answer3, answer4;

    TextView score, question;

    private Questions mQuestions= new Questions();

    private String mAnswer;
    private int mScore = 0;
    private int mQuestionslength=mQuestions.mQuestions.length;

    Random r;


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

        r = new Random();

        answer1 = (Button) findViewById(R.id.answer1);
        answer2 = (Button) findViewById(R.id.answer2);
        answer3 = (Button) findViewById(R.id.answer3);
        answer4 = (Button) findViewById(R.id.answer4);

        score = (TextView) findViewById(R.id.score);
        question = (TextView) findViewById(R.id.question);

        updateQuestion(r.nextInt(mQuestionslength));

        answer1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            if(answer1.getText()==mAnswer) {
                mScore++;
                score.setText("Score " + mScore);
                updateQuestion(r.nextInt(mQuestionslength));
            }else{
                    gameOver();
                }
            }
            }
        );

        answer2.setOnClickListener(new View.OnClickListener() {
                                       @Override
                                       public void onClick(View view) {
                                           if (answer2.getText() == mAnswer) {
                                               mScore++;
                                               score.setText("Score " + mScore);
                                               updateQuestion(r.nextInt(mQuestionslength));
                                           } else {
                                               gameOver();
                                           }
                                       }
                                   }
        );
        answer3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(answer3.getText()==mAnswer) {
                        mScore++;
                        score.setText("Score " + mScore);
                        updateQuestion(r.nextInt(mQuestionslength));
                    }
                    else{
                        gameOver();
                    }
                }

            }
        );

        answer4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (answer4.getText() == mAnswer) {
                    mScore++;
                    score.setText("Score " + mScore);
                    updateQuestion(r.nextInt(mQuestionslength));
                } else {
                    gameOver();
                }
            }

        }
        );
    }

    private void updateQuestion(int num) {
        question.setText(mQuestions.getQuestion(num));
        answer1.setText(mQuestions.getChoice1(num));
        answer2.setText(mQuestions.getChoice2(num));
        answer3.setText(mQuestions.getChoice3(num));
        answer4.setText(mQuestions.getChoice4(num));

        mAnswer=mQuestions.getCorrectAnswer(num);
    }
    private void gameOver(){
        AlertDialog.Builder alertDiaLogBuilder= new AlertDialog.Builder(MainActivity.this);
        alertDiaLogBuilder
                .setMessage("GameOver! Your Score is" +mScore+" Points.")
                .setCancelable(false)
                .setPositiveButton("New Game",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                startActivity(new Intent(getApplication(),MainActivity.class));
                            }
                        }
                )
                .setNegativeButton("EXIT",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    finish();
                    }
                }
        );

    }
}




***********************************************************************************************************************

Questions.java

********************************************
package com.example.hp_pc.hpluniv;

/**
 * Quiz app
 */

public class Questions {
    public String mQuestions[]={

            "which is the first planet in the solar system?",
            "which is the second planet in the solar system?",
            "which is the third planet in the solar system?",
            "which is the fourth planet in the solar system?",
            "which is the fifth planet in the solar system?",
            "which is the sixth planet in the solar system?",
            "which is the seventh planet in the solar system?",
            "which is the eighth planet in the solar system?",
            "which is the nineth planet in the solar system?"
    };

    private String mChoices[][]={

            {"Mercury","Venus","Mars","Saturn"},
            {"Jupitor","Venus","Earth","Neptune"},
            {"Earth","Jupitor","Pluto","Venus"},
            {"Jupitor","Saturn","Mars","Earth"},
            {"Jupitor","Pluto","Mercury","Earth"},
            {"Uranus","Venus","Mars","Saturn"},
            {"Saturn","Pluto","Uranous","Earth"},
            {"Venus","Neptune","Pluto","Mars"},
            {"Mercury","Venus","Mars","Pluto"}
    };

    private String mCorrectAnswers[]={"Mercury","Venus","Earth","Mars","Jupitor","Saturn","Uranous","Neptune","Pluto"};

    public String getQuestion(int a){
        String question=mQuestions[a];
        return question;
    }
    public String getChoice1(int a){
            String choice=mChoices[a][0];
            return choice;
    }
    public String getChoice2(int a){
        String choice=mChoices[a][1];
        return choice;
    }
    public String getChoice3(int a){
        String choice=mChoices[a][2];
        return choice;
    }
    public String getChoice4(int a){
        String choice=mChoices[a][3];
        return choice;
    }
    public String getCorrectAnswer(int a){
        String answer=mCorrectAnswers[a];
        return answer;
    }
}


**********************************************************
activity_main.xml
**********************************************************
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context="com.example.hp_pc.hpluniv.MainActivity">

    <Button
        android:id="@+id/answer4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/answer3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/answer4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/answer2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/answer3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/answer1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/answer2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/score"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="TextView"
        android:gravity="center"
        android:textSize="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/question"
        android:layout_width="376dp"
        android:layout_height="233dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:gravity="center"
        app:layout_constraintBottom_toTopOf="@+id/answer1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/score" />
</android.support.constraint.ConstraintLayout>


*************************************************************
strings.xml
****************************************
<resources>
    <string name="app_name">HPLUNI</string>
</resources>


*********************************************
styles.xml
********************************************
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>






Comments

Popular posts from this blog

Android studio 8 Radio Group and Radio Buttons

Android studio 2 Creation of a simple app