title: Disable and Enable button according to certain conditions
body: <p>I'm relatively new to android programming . Currently using eclipse and I have a problem .</p>
  
  <p>I'm asked to code a button called clockin , whereby this button will check if "Allow non-working site" to clock in . Meaning to say that when opening up this application , this button will automatically check if it allows non-working site . If it allows , it will do authentication , if not it will check its distance between the worksite and current location . If within range , enable button , if not disable the button .</p>
  
  <p>This may sound rather complex but I'm task to do this ASAP .</p>
  
  <p>The code I have is as follow :</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  EditText txtusername;
  EditText txtpassword;
  Button btnclockin;
  Button btnclockout;
  GPSTracker gps;
  
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      txtusername=(EditText)this.findViewById(R.id.username);
      txtpassword=(EditText)this.findViewById(R.id.password);
      btnclockin=(Button)this.findViewById(R.id.clockin);
      btnclockout=(Button)this.findViewById(R.id.clockout);
      btnclockin.setOnClickListener(new OnClickListener() {
  
  public void onClick(View v) {
  // TODO Auto-generated method stub
  
  if((txtusername.getText().toString()).equals(txtpassword.getText().toString())){
  
  Calendar c1 = Calendar.getInstance();
  SimpleDateFormat sdf1 = new SimpleDateFormat("dd/MM/yyyy   hh:mm");
  String strdate1 = sdf1.format(c1.getTime());
  
  TextView txtdate1 = (TextView) findViewById(R.id.textView1);
  txtdate1.setText(strdate1);
  
  gps = new GPSTracker(MainActivity.this);
  
  // check if GPS enabled
  if(gps.canGetLocation()){
  
      double latitude = gps.getLatitude();
      double longitude = gps.getLongitude();
  
      // \n is for new line
      Toast.makeText(getApplicationContext(), "You have clock in succesfully at \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
  }else{
      // can't get location
      // GPS or Network is not enabled
      // ask user to enable GPS/Network in settings
      gps.showSettingsAlert();
  
  }
     } else{
      Toast.makeText(MainActivity.this, "Invalid Username//Password",Toast.LENGTH_LONG).show();
     }
  
  }
  }) ;     
  
  
  
  btnclockout.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
      // TODO Auto-generated method stub
  
      if((txtusername.getText().toString()).equals(txtpassword.getText().toString())){
  
          Calendar c1 = Calendar.getInstance();
          SimpleDateFormat sdf1 = new SimpleDateFormat("d/M/yy h:m a");
          String strdate1 = sdf1.format(c1.getTime());
  
          TextView txtdate1 = (TextView) findViewById(R.id.textView1);
          txtdate1.setText(strdate1);
  
          gps = new GPSTracker(MainActivity.this);
  
          // check if GPS enabled
          if(gps.canGetLocation()){
  
  
              double latitude = gps.getLatitude();
              double longitude = gps.getLongitude();
  
  
              // \n is for new line
              Toast.makeText(getApplicationContext(), "You have clocked out successfully at \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
          }else{
              // can't get location
              // GPS or Network is not enabled
              // ask user to enable GPS/Network in settings
              gps.showSettingsAlert();
  
          }
  
             } else{
              Toast.makeText(MainActivity.this, "Invalid Username//Password",Toast.LENGTH_LONG).show();
             }
  
  
  
  
  
  };
  });
  } 
  
  
  }
  </code></pre>