You are on page 1of 15

EX.

NO:12 ANDROID SIMPLE MATH QUIZ APP PROJECT


DATE:

AIM: To develop a android project of simple math quiz app


PROCEDURE:
STEP1: Creating new project

STEP2: Designing the UI with activity_main.xml

STEP3: Working with question.java

STEP4: Working with strings.xml file

STEP5: Working with mainactivity.java

ABSTRACT:
Android is a operating system which is built basically
for mobile phones it is based on linux kernel and other
open-source software and is developed by google.
In this project lets create a simple math quiz app in
android using java. A simple quiz app that contains set of

211519104163 SUKESH REDDY R


curated questions and its answers given by the user it
navigates through the questions using dynamic
programing.

211519104163 SUKESH REDDY R


SOURCE CODE:
Activity.main:
This XML file does not appear to have any style information
associated with it. The document tree is shown below.
<androidx.constraintlayout.widget.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=".MainActivity">
<Button android:id="@+id/btnStart" android:layout_width="139dp"
android:layout_height="114dp" android:background="#4CAF50"
android:onClick="start" android:text="Start" android:textColor="#FFFEFE"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/lastUi" android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView android:id="@+id/FinalscoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="46dp"
android:layout_marginBottom="278dp" android:background="#4CAF50"
android:padding="10dp" android:text="0/0" android:textColor="#F4F0F0"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"

211519104163 SUKESH REDDY R


app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView4"/>
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="192dp" android:text="- Game Over -"
android:textColor="#9A1818" android:textSize="36sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp" android:text="Your Score"
android:textSize="24sp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"/>
<Button android:id="@+id/buttonPlayAgain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:layout_marginBottom="143dp" android:onClick="playAgain"
android:text="Play Again"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/scoreTextView"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/quizUi" android:layout_width="match_parent"

211519104163 SUKESH REDDY R


android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<Button android:id="@+id/button0" android:layout_width="140dp"
android:layout_height="105dp" android:layout_marginStart="28dp"
android:layout_marginLeft="28dp" android:layout_marginTop="46dp"
android:layout_marginBottom="29dp" android:background="#9BE7F1"
android:onClick="optionSelect" android:tag="0" android:text="Button"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/QuestionTextView"/>
<Button android:id="@+id/button1" android:layout_width="140dp"
android:layout_height="105dp" android:layout_marginStart="240dp"
android:layout_marginLeft="240dp" android:layout_marginTop="46dp"
android:layout_marginEnd="31dp" android:layout_marginRight="31dp"
android:layout_marginBottom="29dp" android:background="#9BE7F1"
android:onClick="optionSelect" android:tag="1" android:text="Button"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/QuestionTextView"/>
<Button android:id="@+id/button2" android:layout_width="140dp"
android:layout_height="105dp" android:layout_marginStart="28dp"
android:layout_marginLeft="28dp" android:layout_marginTop="29dp"
android:layout_marginEnd="72dp" android:layout_marginRight="72dp"
android:layout_marginBottom="218dp" android:background="#9BE7F1"
android:onClick="optionSelect" android:tag="2" android:text="Button"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"

211519104163 SUKESH REDDY R


app:layout_constraintEnd_toStartOf="@+id/button3"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button0"/>
<Button android:id="@+id/button3" android:layout_width="140dp"
android:layout_height="105dp" android:layout_marginStart="240dp"
android:layout_marginLeft="240dp" android:layout_marginTop="29dp"
android:layout_marginEnd="31dp" android:layout_marginRight="31dp"
android:layout_marginBottom="218dp" android:background="#9BE7F1"
android:onClick="optionSelect" android:tag="3" android:text="Button"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1"/>
<TextView android:id="@+id/ScoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp" android:layout_marginEnd="28dp"
android:layout_marginRight="28dp" android:background="#4CAF50"
android:padding="10dp" android:text="0/0" android:textColor="#F4F0F0"
android:textSize="30sp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView android:id="@+id/QuestionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:padding="20dp"
android:text="29+12" android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.207"/>

211519104163 SUKESH REDDY R


<TextView android:id="@+id/TimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp" android:layout_marginLeft="28dp"
android:layout_marginTop="72dp" android:background="#FF9800"
android:padding="10dp" android:text="30s" android:textColor="#FDFCFC"
android:textSize="30sp" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView android:id="@+id/AlertTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="335dp"
android:layout_marginBottom="149dp" android:text="Let's Do it!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/QuestionTextView"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Activity.java:
package com.astro.math

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;

211519104163 SUKESH REDDY R


import android.widget.TextView;

import java.lang.reflect.Array;
import java.sql.Time;

import java.util.ArrayList;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

Button startBtn;
TextView TimeTextView;
TextView ScoreTextView;

TextView AlertTextView;
TextView QuestionTextView;

TextView FinalScoreTextView;
Button button0;
Button button1;

Button button2;
Button button3;

CountDownTimer countDownTimer;
ConstraintLayout constraintLayout;

ConstraintLayout lastLayout;
Button buttonPlayAgain;

Random random =new Random();

int a;

int b;
int indexOfCorrectAnswer;
ArrayList<Integer> answers = new ArrayList<Integer>();

211519104163 SUKESH REDDY R


int points = 0;
int totalQuestions = 0;

@SuppressLint("SetTextI18n")
@Override

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn=(Button)findViewById(R.id.btnStart);

TimeTextView = findViewById(R.id.TimeTextView);

ScoreTextView = findViewById(R.id.ScoreTextView);
FinalScoreTextView=findViewById(R.id.FinalscoreTextView);

AlertTextView = findViewById(R.id.AlertTextView);
QuestionTextView =findViewById(R.id.QuestionTextView);

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

buttonPlayAgain=findViewById(R.id.buttonPlayAgain);

constraintLayout=findViewById(R.id.quizUi);

lastLayout=findViewById(R.id.lastUi);

lastLayout.setVisibility(View.INVISIBLE);

constraintLayout.setVisibility(View.INVISIBLE);

211519104163 SUKESH REDDY R


}

@SuppressLint("SetTextI18n")
public void NextQuestion(){
a = random.nextInt(10);

b = random.nextInt(10);

QuestionTextView.setText(a+"+"+b);

indexOfCorrectAnswer = random.nextInt(4);
answers.clear();
for(int i = 0; i<4; i++){

if(indexOfCorrectAnswer == i){
answers.add(a+b);

}else {
int wrongAnswer = random.nextInt(20);
while(wrongAnswer==a+b){

wrongAnswer = random.nextInt(20);
}

answers.add(wrongAnswer);
}
}

button0.setText(Integer.toString(answers.get(0)));
button1.setText(Integer.toString(answers.get(1)));
button2.setText(Integer.toString(answers.get(2)));
button3.setText(Integer.toString(answers.get(3)));

211519104092 NALLA NAGARAJULU REDDY


}

public void optionSelect(View view){


totalQuestions++;
if(Integer.toString(indexOfCorrectAnswer).equals(view.getTag().toString())){

points++;

AlertTextView.setText("Correct");

}else {
AlertTextView.setText("Wrong");
}

ScoreTextView.setText(Integer.toString(points)+"/"+Integer.toString(totalQuestions));

NextQuestion();
}

public void playAgain(View view){

points=0;
totalQuestions=0;

ScoreTextView.setText(Integer.toString(points)+"/"+Integer.toString(totalQuestions));
countDownTimer.start();
lastLayout.setVisibility(View.INVISIBLE);

constraintLayout.setVisibility(View.VISIBLE);

211519104092 NALLA NAGARAJULU REDDY


public void start(View view) {
startBtn.setVisibility(View.INVISIBLE);
constraintLayout.setVisibility(View.VISIBLE);

NextQuestion();
countDownTimer = new CountDownTimer(30000,1000) {
@Override
public void onTick(long millisUntilFinished) {
TimeTextView.setText(String.valueOf(millisUntilFinished/1000)+"s");
}

@Override

public void onFinish() {


TimeTextView.setText("Time Up!");

FinalScoreTextView.setText(Integer.toString(points)+"/"+Integer.toString(totalQuestions));
constraintLayout.setVisibility(View.INVISIBLE);

lastLayout.setVisibility(View.VISIBLE);

}
}.start();

}
}

211519104092 NALLA NAGARAJULU REDDY


OUTPUT:

211519104092 NALLA NAGARAJULU REDDY


211519104092 NALLA NAGARAJULU REDDY
RESULT: Thus the android application of simple math quiz
was executed successfully

211519104092 NALLA NAGARAJULU REDDY

You might also like