Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package midterm.garcia.com.midtermexam;
- import android.support.v4.app.Fragment;
- import android.os.Bundle;
- import android.support.annotation.Nullable;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.Toast;
- import java.util.Random;
- import java.util.ArrayList;
- import static midterm.garcia.com.midtermexam.R.id.answerBtn;
- import static midterm.garcia.com.midtermexam.R.id.surrenderBtn;
- import static midterm.garcia.com.midtermexam.R.id.hintBtn;
- public class Shapes extends Fragment implements View.OnClickListener{
- ArrayList<String> shapes = new ArrayList<String>();
- ArrayList<String> hints = new ArrayList<String>();
- ArrayList<Integer> numbers = new ArrayList<Integer>();
- Random randomizer = new Random();
- String answer, hint;
- boolean checker = false;
- private int i;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
- return inflater.inflate(R.layout.fragment_shapes, container, false);
- }
- @Override
- public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- getActivity().setTitle("Guess the shape!");
- Button surrenderBtn = getView().findViewById(R.id.surrenderBtn);
- Button hintBtn = getView().findViewById(R.id.hintBtn);
- Button answerBtn = getView().findViewById(R.id.answerBtn);
- hintBtn.setOnClickListener(Shapes.this);
- answerBtn.setOnClickListener(Shapes.this);
- surrenderBtn.setOnClickListener(Shapes.this);
- hintList();
- createShapeList();
- createAnswer();
- setImageShape();
- }
- public void setImageShape(){
- ImageView imageView1 = getView().findViewById(R.id.imageView1);
- int res = getResources().getIdentifier(answer, "drawable", "midterm.garcia.com.midtermexam");
- imageView1.setImageResource(res);
- }
- public void createAnswer() {
- int rand = randomizer.nextInt(7);
- for (i = 0; i <= rand; i++) {
- answer = shapes.get(i);
- hint = hints.get(i);
- }
- }
- public void checkUserInput(){
- EditText ansEditText = getView().findViewById(R.id.ansEditText);
- Button surrenderBtn = getView().findViewById(R.id.surrenderBtn);
- String userInput = ansEditText.getText().toString().toLowerCase();
- if ( userInput.equals(answer) ) {
- Toast.makeText(getActivity(),"Great Job! Tap next to guess another",Toast.LENGTH_LONG).show();
- surrenderBtn.setText("Next");
- }
- else{
- Toast.makeText(getActivity(),"Try again!",Toast.LENGTH_SHORT).show();
- surrenderBtn.setText("Skip");
- }
- }
- public void createShapeList(){
- shapes.add("circle");
- shapes.add("box");
- shapes.add("triangle");
- shapes.add("hexagon");
- shapes.add("rectangle");
- shapes.add("star");
- shapes.add("heart");
- }
- public void hintList(){
- hints.add("It has no sides and it starts with the letter \"C\"!");
- hints.add("It has 4 sides and its sides are equal with each other. Starts with the letter \"S\"!");
- hints.add("It has 3 POINTY sides and starts with the letter \"T\"!");
- hints.add("It is a polygon figure with 8 sides and it starts with the letter \"O\" and ends with a \"gon\" !");
- hints.add("It has 4 sides but two sides but there are two unequal sides that are parallel to each other. Starts with \"R\"");
- hints.add("It has 10 sides and it goes with \"Twinkle Twinkle Little _ _ _ _ \", and it starts with \"S\" ");
- hints.add("It has 2 sides and 2 curved lines on its sides. They say its a symbol of love. And starts with \"H\"");
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()){
- case hintBtn:{
- hints();
- break;
- }
- case answerBtn:{
- checkUserInput();
- break;
- }
- case surrenderBtn:{
- createAnswer();
- setImageShape();
- break;
- }
- }
- }
- public void hints(){
- Toast.makeText(getActivity(), hints.get(i), Toast.LENGTH_LONG).show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement