deyanmalinov

Android / Deyan's Guessing Game

Jun 14th, 2021 (edited)
324
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---MainActivity.java---
  2.  
  3. package com.example.deyansguessinggame;
  4.  
  5. import androidx.appcompat.app.AppCompatActivity;
  6.  
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15.     private EditText edtNumber;
  16.     private Button button;
  17.     private TextView message;
  18.  
  19.     private int theNumber;
  20.  
  21.     public void gameEngine(){
  22.         String numEnt = edtNumber.getText().toString();
  23.         String msg="";
  24.         int guess = Integer.parseInt(numEnt);
  25.         try {
  26.             if (guess > theNumber) {
  27.                  msg = guess + " was too high. Try again!";
  28.                  message.setText(msg);
  29.              } else if (guess < theNumber) {
  30.                  msg = guess + " was too low. Try again!";
  31.                  message.setText(msg);
  32.              } else {
  33.                  message.setText("Correct. You Win!!!");
  34.                  newGame();
  35.              }
  36.          }
  37.         catch (Exception excp){
  38.             message.setText("The number has to be between 1 and 100");
  39.             edtNumber.setText(String.valueOf(0));
  40.         }finally {
  41.             edtNumber.requestFocus();
  42.             edtNumber.selectAll();
  43.         }
  44.     }
  45.  
  46.     @Override
  47.     protected void onCreate(Bundle savedInstanceState) {
  48.         super.onCreate(savedInstanceState);
  49.         setContentView(R.layout.activity_main);
  50.         edtNumber = (EditText) findViewById(R.id.editNumber);
  51.         button = (Button) findViewById(R.id.button);
  52.         message = (TextView) findViewById(R.id.textMessage);
  53.         newGame();
  54.         button.setOnClickListener(new View.OnClickListener() {
  55.             @Override
  56.             public void onClick(View v) {
  57.                 gameEngine();
  58.             }
  59.         });
  60.     }
  61.  
  62.     public void newGame(){
  63.         theNumber = (int) (Math.random() * 100 + 1
  64.  
  65. *********************************************************************************************
  66.  
  67. ---activity_main.xml---
  68.  
  69. <?xml version="1.0" encoding="utf-8"?>
  70. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  71.     xmlns:app="http://schemas.android.com/apk/res-auto"
  72.     xmlns:tools="http://schemas.android.com/tools"
  73.     android:layout_width="match_parent"
  74.     android:layout_height="match_parent"
  75.     tools:context=".MainActivity">
  76.  
  77.     <TextView
  78.         android:id="@+id/textView"
  79.         android:layout_width="wrap_content"
  80.         android:layout_height="wrap_content"
  81.         android:layout_marginTop="68dp"
  82.         android:text="Deyan's Guessing Game"
  83.         android:textColor="#3F51B5"
  84.         android:textSize="30sp"
  85.         android:textStyle="bold"
  86.         app:layout_constraintEnd_toEndOf="parent"
  87.         app:layout_constraintHorizontal_bias="0.494"
  88.         app:layout_constraintStart_toStartOf="parent"
  89.         app:layout_constraintTop_toTopOf="parent" />
  90.  
  91.     <TextView
  92.         android:id="@+id/textView2"
  93.         android:layout_width="wrap_content"
  94.         android:layout_height="wrap_content"
  95.         android:layout_marginTop="164dp"
  96.         android:text="Enter a number between 1 and 100:"
  97.         android:textSize="18sp"
  98.         app:layout_constraintEnd_toEndOf="parent"
  99.         app:layout_constraintStart_toStartOf="parent"
  100.         app:layout_constraintTop_toTopOf="parent" />
  101.  
  102.     <Button
  103.         android:id="@+id/button"
  104.         android:layout_width="138dp"
  105.         android:layout_height="62dp"
  106.         android:layout_marginTop="332dp"
  107.         android:text="GUESS"
  108.         android:textSize="24sp"
  109.         app:backgroundTint="#CDDC39"
  110.         app:layout_constraintEnd_toEndOf="parent"
  111.         app:layout_constraintHorizontal_bias="0.498"
  112.         app:layout_constraintStart_toStartOf="parent"
  113.         app:layout_constraintTop_toTopOf="parent" />
  114.  
  115.     <EditText
  116.         android:id="@+id/editNumber"
  117.         android:layout_width="56dp"
  118.         android:layout_height="43dp"
  119.         android:layout_marginTop="236dp"
  120.         android:ems="10"
  121.         android:inputType="number"
  122.         android:textAlignment="center"
  123.         app:layout_constraintEnd_toEndOf="parent"
  124.         app:layout_constraintStart_toStartOf="parent"
  125.         app:layout_constraintTop_toTopOf="parent" />
  126.  
  127.     <TextView
  128.         android:id="@+id/textMessage"
  129.         android:layout_width="wrap_content"
  130.         android:layout_height="wrap_content"
  131.         android:layout_marginTop="444dp"
  132.         android:text="Enter a number and then click GUESS"
  133.         android:textSize="16sp"
  134.         app:layout_constraintEnd_toEndOf="parent"
  135.         app:layout_constraintStart_toStartOf="parent"
  136.         app:layout_constraintTop_toTopOf="parent" />
  137. </androidx.constraintlayout.widget.ConstraintLayout>
RAW Paste Data