Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. package midterm.garcia.com.midtermexam;
  2.  
  3. import android.support.v4.app.Fragment;
  4. import android.os.Bundle;
  5. import android.support.annotation.Nullable;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.ImageView;
  12. import android.widget.Toast;
  13. import java.util.Random;
  14. import java.util.ArrayList;
  15. import static midterm.garcia.com.midtermexam.R.id.answerBtn;
  16. import static midterm.garcia.com.midtermexam.R.id.surrenderBtn;
  17. import static midterm.garcia.com.midtermexam.R.id.hintBtn;
  18.  
  19.  
  20. public class Shapes extends Fragment implements View.OnClickListener{
  21. ArrayList<String> shapes = new ArrayList<String>();
  22. ArrayList<String> hints = new ArrayList<String>();
  23. ArrayList<Integer> numbers = new ArrayList<Integer>();
  24. Random randomizer = new Random();
  25. String answer, hint;
  26. boolean checker = false;
  27. private int i;
  28. @Nullable
  29. @Override
  30. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
  31. return inflater.inflate(R.layout.fragment_shapes, container, false);
  32.  
  33. }
  34. @Override
  35. public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
  36. super.onViewCreated(view, savedInstanceState);
  37. getActivity().setTitle("Guess the shape!");
  38. Button surrenderBtn = getView().findViewById(R.id.surrenderBtn);
  39. Button hintBtn = getView().findViewById(R.id.hintBtn);
  40. Button answerBtn = getView().findViewById(R.id.answerBtn);
  41. hintBtn.setOnClickListener(Shapes.this);
  42. answerBtn.setOnClickListener(Shapes.this);
  43. surrenderBtn.setOnClickListener(Shapes.this);
  44. hintList();
  45. createShapeList();
  46. createAnswer();
  47. setImageShape();
  48. }
  49.  
  50. public void setImageShape(){
  51. ImageView imageView1 = getView().findViewById(R.id.imageView1);
  52. int res = getResources().getIdentifier(answer, "drawable", "midterm.garcia.com.midtermexam");
  53. imageView1.setImageResource(res);
  54. }
  55.  
  56. public void createAnswer() {
  57. int rand = randomizer.nextInt(7);
  58. for (i = 0; i <= rand; i++) {
  59. answer = shapes.get(i);
  60. hint = hints.get(i);
  61.  
  62. }
  63. }
  64.  
  65.  
  66.  
  67. public void checkUserInput(){
  68. EditText ansEditText = getView().findViewById(R.id.ansEditText);
  69. Button surrenderBtn = getView().findViewById(R.id.surrenderBtn);
  70. String userInput = ansEditText.getText().toString().toLowerCase();
  71. if ( userInput.equals(answer) ) {
  72. Toast.makeText(getActivity(),"Great Job! Tap next to guess another",Toast.LENGTH_LONG).show();
  73. surrenderBtn.setText("Next");
  74. }
  75. else{
  76. Toast.makeText(getActivity(),"Try again!",Toast.LENGTH_SHORT).show();
  77. surrenderBtn.setText("Skip");
  78. }
  79. }
  80.  
  81. public void createShapeList(){
  82. shapes.add("circle");
  83. shapes.add("box");
  84. shapes.add("triangle");
  85. shapes.add("hexagon");
  86. shapes.add("rectangle");
  87. shapes.add("star");
  88. shapes.add("heart");
  89.  
  90. }
  91.  
  92. public void hintList(){
  93. hints.add("It has no sides and it starts with the letter \"C\"!");
  94. hints.add("It has 4 sides and its sides are equal with each other. Starts with the letter \"S\"!");
  95. hints.add("It has 3 POINTY sides and starts with the letter \"T\"!");
  96. hints.add("It is a polygon figure with 8 sides and it starts with the letter \"O\" and ends with a \"gon\" !");
  97. hints.add("It has 4 sides but two sides but there are two unequal sides that are parallel to each other. Starts with \"R\"");
  98. hints.add("It has 10 sides and it goes with \"Twinkle Twinkle Little _ _ _ _ \", and it starts with \"S\" ");
  99. hints.add("It has 2 sides and 2 curved lines on its sides. They say its a symbol of love. And starts with \"H\"");
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. @Override
  108. public void onClick(View v) {
  109.  
  110. switch (v.getId()){
  111. case hintBtn:{
  112. hints();
  113. break;
  114. }
  115. case answerBtn:{
  116. checkUserInput();
  117. break;
  118. }
  119. case surrenderBtn:{
  120. createAnswer();
  121. setImageShape();
  122.  
  123. break;
  124. }
  125. }
  126. }
  127. public void hints(){
  128. Toast.makeText(getActivity(), hints.get(i), Toast.LENGTH_LONG).show();
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement