Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---MainActivity.java---
- package com.example.deyansguessinggame;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- private EditText edtNumber;
- private Button button;
- private TextView message;
- private int theNumber;
- public void gameEngine(){
- String numEnt = edtNumber.getText().toString();
- String msg="";
- int guess = Integer.parseInt(numEnt);
- try {
- if (guess > theNumber) {
- msg = guess + " was too high. Try again!";
- message.setText(msg);
- } else if (guess < theNumber) {
- msg = guess + " was too low. Try again!";
- message.setText(msg);
- } else {
- message.setText("Correct. You Win!!!");
- newGame();
- }
- }
- catch (Exception excp){
- message.setText("The number has to be between 1 and 100");
- edtNumber.setText(String.valueOf(0));
- }finally {
- edtNumber.requestFocus();
- edtNumber.selectAll();
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- edtNumber = (EditText) findViewById(R.id.editNumber);
- button = (Button) findViewById(R.id.button);
- message = (TextView) findViewById(R.id.textMessage);
- newGame();
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- gameEngine();
- }
- });
- }
- public void newGame(){
- theNumber = (int) (Math.random() * 100 + 1
- *********************************************************************************************
- ---activity_main.xml---
- <?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">
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="68dp"
- android:text="Deyan's Guessing Game"
- android:textColor="#3F51B5"
- android:textSize="30sp"
- android:textStyle="bold"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.494"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="164dp"
- android:text="Enter a number between 1 and 100:"
- android:textSize="18sp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <Button
- android:id="@+id/button"
- android:layout_width="138dp"
- android:layout_height="62dp"
- android:layout_marginTop="332dp"
- android:text="GUESS"
- android:textSize="24sp"
- app:backgroundTint="#CDDC39"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.498"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/editNumber"
- android:layout_width="56dp"
- android:layout_height="43dp"
- android:layout_marginTop="236dp"
- android:ems="10"
- android:inputType="number"
- android:textAlignment="center"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/textMessage"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="444dp"
- android:text="Enter a number and then click GUESS"
- android:textSize="16sp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
Add Comment
Please, Sign In to add comment