Android Open Source - guess-num Main Activity






From Project

Back to project page guess-num.

License

The source code is released under:

GNU General Public License

If you think the Android project guess-num listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lookjohn.guessnumber;
/*from   ww  w . j  a v  a 2 s  .com*/
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
  Random random; 
  Button button;
  EditText text;
  
  int input; 
  int MIN, MAX;
  int comp; 
  int guesses;

  public void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    random = new Random();
    button = (Button)findViewById(R.id.button1);
    text = (EditText)findViewById(R.id.editText1);
    
    MIN = 1;
    MAX = 100;
    comp = random.nextInt(MAX - MIN + 1) + MIN; // Generate random number between 1 and 100.
    guesses = 0;
        
    button.setOnClickListener(myhandler1);  
    
  }
    
  View.OnClickListener myhandler1 = new View.OnClickListener() {
    
    public void onClick(View v) {
      
      String value = text.getText().toString(); // Get value from input from editTextView
      
      // If user submits an empty EditText, return to prevent a crash.
      // Doesn't output anything strangely.
      if (value.isEmpty()) {
        Toast.makeText(MainActivity.this, 
            getString(R.string.please), 
            Toast.LENGTH_SHORT);
        return;
      }
      
      // Prevents any input greater than 4 digits to be used.
      if (value.length() > 4) {
        Toast toast = Toast.makeText(MainActivity.this, 
            R.string.over_guess, 
            Toast.LENGTH_SHORT);
          toast.setGravity(Gravity.CENTER, 0, 0);  
          toast.show();        
        return;
      }
      
      input = Integer.parseInt(value); // Turn string into integer
      guesses++;
      
      if(input > 100) {  
        Toast toast = Toast.makeText(MainActivity.this, 
          R.string.over_guess, 
          Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);  
        toast.show();  
        return;
        }
      else if(input < comp) {
        Toast toast = Toast.makeText(MainActivity.this, 
          getString(R.string.guess_low, input), 
          Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);  
        toast.show();
      }
      else if(input > comp) {
        Toast toast = Toast.makeText(MainActivity.this, 
          getString(R.string.guess_high, input), 
          Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);  
        toast.show();
      }
      else {
        Toast toast = Toast.makeText(MainActivity.this, 
          getString(R.string.correct, comp, guesses), 
          Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);  
        toast.show();
        guesses--; // Guess count won't increase.
      }
    }
  };
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }
  
}




Java Source Code List

com.lookjohn.guessnumber.MainActivity.java