Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. package com.example.higherorlower;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. import java.util.Random;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15. public void Go(View view) {
  16.  
  17. EditText enteredNumber = findViewById(R.id.enteredNumber);
  18. String entryAsString = enteredNumber.getText().toString();
  19. // int entryAsInt = Integer.parseInt(entryAsString);
  20. if (!entryAsString.equals(""))
  21. if (Integer.parseInt(entryAsString) >= 0 && Integer.parseInt(entryAsString) <= 20)
  22. if (Integer.parseInt(entryAsString) > randomNum) {
  23. Toast.makeText(this, "Choose a lesser number", Toast.LENGTH_SHORT).show();
  24. enteredNumber.setText("");
  25. } else if (Integer.parseInt(entryAsString) < randomNum) {
  26. Toast.makeText(this, "Choose a higher number", Toast.LENGTH_SHORT).show();
  27. enteredNumber.setText("");
  28. } else {
  29. Toast.makeText(this, "Bingo! " + randomNum + " was the number I was thinking", Toast.LENGTH_SHORT).show();
  30. random1();
  31. enteredNumber.setText("");
  32.  
  33. }
  34. else {
  35. Toast.makeText(this, "You are guessing number out of the limit!", Toast.LENGTH_LONG).show();
  36. enteredNumber.setText("");
  37. }
  38. else {
  39. // Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
  40. }
  41. }
  42.  
  43. int randomNum;
  44.  
  45. public void random1() {
  46. Random r = new Random();
  47. randomNum = r.nextInt(20);
  48. }
  49.  
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.activity_main);
  54. random1();
  55. }
  56. }
  57.  
  58.  
  59. ******************************************************************************
  60.  
  61. <?xml version="1.0" encoding="utf-8"?>
  62. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  63. xmlns:app="http://schemas.android.com/apk/res-auto"
  64. xmlns:tools="http://schemas.android.com/tools"
  65. android:layout_width="match_parent"
  66. android:layout_height="match_parent"
  67. android:onClick="Go"
  68. tools:context=".MainActivity">
  69.  
  70. <TextView
  71. android:id="@+id/textView"
  72. android:layout_width="wrap_content"
  73. android:layout_height="wrap_content"
  74. android:layout_marginStart="37dp"
  75. android:layout_marginTop="63dp"
  76. android:layout_marginEnd="36dp"
  77. android:layout_marginBottom="558dp"
  78. android:text="@string/i_have_a_number_in_my_mind_between_0_and_20_can_you_guess_it"
  79. android:textAlignment="center"
  80. android:textSize="16sp"
  81. app:layout_constraintBottom_toBottomOf="parent"
  82. app:layout_constraintEnd_toEndOf="parent"
  83. app:layout_constraintStart_toStartOf="parent"
  84. app:layout_constraintTop_toTopOf="parent" />
  85.  
  86. <EditText
  87. android:id="@+id/enteredNumber"
  88. android:layout_width="329dp"
  89. android:layout_height="51dp"
  90. android:layout_marginStart="10dp"
  91. android:layout_marginEnd="10dp"
  92. android:layout_marginBottom="452dp"
  93. android:ems="10"
  94. android:hint="@string/enter_the_number_you_re_guessing"
  95. android:inputType="number"
  96. app:layout_constraintBottom_toBottomOf="parent"
  97. app:layout_constraintEnd_toEndOf="parent"
  98. app:layout_constraintHorizontal_bias="0.545"
  99. app:layout_constraintStart_toStartOf="parent"
  100. android:importantForAutofill="no" />
  101.  
  102. <Button
  103. android:id="@+id/button"
  104. android:layout_width="wrap_content"
  105. android:layout_height="wrap_content"
  106. android:layout_marginStart="135dp"
  107. android:layout_marginTop="23dp"
  108. android:layout_marginEnd="137dp"
  109. android:layout_marginBottom="381dp"
  110. android:clickable="false"
  111. android:onClick="Go"
  112. android:soundEffectsEnabled="false"
  113. android:text="@string/enter"
  114. app:layout_constraintBottom_toBottomOf="parent"
  115. app:layout_constraintEnd_toEndOf="parent"
  116. app:layout_constraintStart_toStartOf="parent"
  117. app:layout_constraintTop_toBottomOf="@+id/enteredNumber" />
  118.  
  119. </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement