Brain Tester Application in Android Studio
Hello Everyone! In this Android tutorial, we are going to create a Brain Tester Application in android studio. To perform this task we will be going to use java. We are going to use java to develop a Brain Tester. We can use any version of Android Studio.
Create Brain Tester App in Android Studio
Let’s start by opening the android studio and create a new project.
- First, we need to design the front-end of the application we write code in activity_main.xml.
- Second, we need to providing functionality to our application we write java code in the MainActivity.java file.
- Now start building the project.
<?xml version="1.0" encoding="utf-8"?> <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/Gobutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="105dp" android:layout_marginLeft="105dp" android:layout_marginTop="325dp" android:layout_marginEnd="89dp" android:layout_marginRight="89dp" android:layout_marginBottom="305dp" android:background="#667B1FA2" android:onClick="GoButton" android:padding="30sp" android:text=" GO! " android:textSize="45sp" android:visibility="invisible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/gameLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <GridLayout android:id="@+id/gridLayout" android:layout_width="300dp" android:layout_height="250dp" android:layout_marginTop="90dp" android:background="@android:color/black" android:columnCount="2" android:rowCount="2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.495" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/question"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="0" android:layout_rowWeight="1" android:layout_column="0" android:layout_columnWeight="1" android:layout_gravity="fill" android:layout_margin="4dp" android:background="#FFB5EC84" android:onClick="ChooseAnswer" android:tag="0" android:text="14" android:textSize="30dp" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="0" android:layout_rowWeight="1" android:layout_column="1" android:layout_columnWeight="1" android:layout_gravity="fill" android:layout_margin="4dp" android:background="#FFB5EC84" android:onClick="ChooseAnswer" android:tag="1" android:text="15" android:textSize="30dp" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="1" android:layout_rowWeight="1" android:layout_column="0" android:layout_columnWeight="1" android:layout_gravity="fill" android:layout_margin="4dp" android:background="#FFB5EC84" android:onClick="ChooseAnswer" android:tag="2" android:text="10" android:textSize="30dp" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_row="1" android:layout_rowWeight="1" android:layout_column="1" android:layout_columnWeight="1" android:layout_gravity="fill" android:layout_margin="4dp" android:background="#FFB5EC84" android:onClick="ChooseAnswer" android:tag="3" android:text="5" android:textSize="30dp" /> </GridLayout> <TextView android:id="@+id/question" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="60dp" android:layout_marginLeft="60dp" android:layout_marginTop="16dp" android:layout_marginEnd="60dp" android:layout_marginRight="60dp" android:background="#FBC02D" android:padding="10sp" android:text="10+5" android:textSize="40sp" app:layout_constraintEnd_toStartOf="@+id/score" app:layout_constraintStart_toEndOf="@+id/timer" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/timer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="17dp" android:layout_marginLeft="17dp" android:layout_marginTop="16dp" android:background="#303F9F" android:padding="10sp" android:text="30" android:textColor="@android:color/white" android:textSize="40sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:background="#303F9F" android:padding="10sp" android:text="0/0" android:textColor="@android:color/white" android:textSize="40sp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/correct" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="132dp" android:layout_marginLeft="132dp" android:layout_marginTop="24dp" android:background="#FF53A8D6" android:padding="10dp" android:text="Correct!" android:textColor="@android:color/white" android:textSize="35sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/gridLayout" /> <Button android:id="@+id/playAgain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="88dp" android:layout_marginLeft="88dp" android:layout_marginTop="20dp" android:background="#FBC02D" android:onClick="PlayAgain" android:padding="10dp" android:text="Play Again" android:textSize="30sp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/correct" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Now we are linking XML to java to provide the functionality to our application.
- From XML to Java, we use onClick Listener in Java.
- Define a variable in XML(e.g – android:id=”@+id/button) which is called by onClick Listener.
- In Your Main Activity, we use a Bundle to pass the data between various activities of Android.
- We can map the data from the string value to various data types with the help of parsing using Bundle.
- In this Application we Several functions or methods like the onCreate method of our project, we created objects of TextView, EditText, and button and extent function of java.
- In this app, we use Random variables to generate random.
package com.example.braintestergame; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.Constraints; import android.opengl.Visibility; import android.os.Bundle; import android.os.CountDownTimer; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; import java.util.Random; public class MainActivity extends AppCompatActivity { Button go; ConstraintLayout gameLayout; ArrayList<Integer> options = new ArrayList<Integer>(); TextView resultText; TextView scoreTextView; int score = 0, numberOfQuestions = 0; int setAnswerPosition; TextView questionText; Button playAgainButton; TextView timer; Button option1; Button option2; Button option3; Button option4; public void PlayAgain(final View view) { playAgainButton.setVisibility(view.INVISIBLE); score = 0; numberOfQuestions = 0; timer.setText("30s"); scoreTextView.setText(Integer.toString(numberOfQuestions) + "/" + Integer.toString(score)); newQuestion(); new CountDownTimer(30000 + 100, 1000) { @Override public void onTick(long millisUntilFinished) { timer.setText(String.valueOf(millisUntilFinished / 1000) + "s"); } @Override public void onFinish() { playAgainButton.setVisibility(view.VISIBLE); resultText.setText("Done!"); option1.setClickable(false); option2.setClickable(false); option3.setClickable(false); option4.setClickable(false); timer.setText("0s"); } }.start(); } public void GoButton(View view) { go.setVisibility(view.INVISIBLE); gameLayout.setVisibility(view.VISIBLE); PlayAgain(findViewById(R.id.timer)); } public void ChooseAnswer(View view) { if (Integer.toString(setAnswerPosition).equals(view.getTag().toString())) { resultText.setText("Correct!"); score++; } else { resultText.setText("Wrong :("); } numberOfQuestions++; scoreTextView.setText(Integer.toString(numberOfQuestions) + "/" + Integer.toString(score)); newQuestion(); } public void newQuestion() { Random random = new Random(); int a = random.nextInt(21); int b = random.nextInt(21); questionText = (TextView) findViewById(R.id.question); questionText.setText(Integer.toString(a) + "+" + Integer.toString(b)); setAnswerPosition = random.nextInt(4); options.clear(); for (int i = 0; i < 4; i++) { if (i == setAnswerPosition) { options.add(a + b); } else { int wrongAnswers = random.nextInt(41); while (wrongAnswers == a + b) { wrongAnswers = random.nextInt(41); } options.add(wrongAnswers); } } option1.setText(Integer.toString(options.get(0))); option2.setText(Integer.toString(options.get(1))); option3.setText(Integer.toString(options.get(2))); option4.setText(Integer.toString(options.get(3))); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); go = (Button) findViewById(R.id.Gobutton); gameLayout = (ConstraintLayout) findViewById(R.id.gameLayout); go.setVisibility(View.VISIBLE); gameLayout.setVisibility(View.INVISIBLE); option1 = (Button) findViewById(R.id.button1); option2 = (Button) findViewById(R.id.button2); option3 = (Button) findViewById(R.id.button3); option4 = (Button) findViewById(R.id.button4); playAgainButton = (Button) findViewById(R.id.playAgain); resultText = findViewById(R.id.correct); scoreTextView = findViewById(R.id.score); timer = (TextView) findViewById(R.id.timer); } }
Now, You can understand how to make brain tester game applications using android studio.
Leave a Reply