Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Disabling a button

+16 votes
When clicking on a Submit button, I am checking if both the password and confirm password fields are the same. If they are not, i want to disable to button. Does anyone know how to do that?
asked Apr 27, 2015 in Spring 2015 by Emily Leazer (100 points)

3 Answers

+6 votes
It sounds to me that you want to start with the button disabled, but then have it become enabled once:

a) both the password and the confirm-password edit texts have some text in them (perhaps >= 5 characters... weak security, but probably fine for this app.)

b) the texts match each other.

The key thing, then is to *listen* to the event of text changing in the EditText views, so you can respond to it.  So you'll need to add a listener for that... e.g.:  http://stackoverflow.com/questions/4310525/android-on-edittext-changed-listener
answered Apr 27, 2015 by Forrest Stonedahl (3,438 points)
+4 votes
http://lovelace.augustana.edu/q2a/index.php/658/how-to-make-a-button-unclickable-for-a-time

 

Check out this thread.  I assume it is the same kind of thing that you are trying to get.  Basically if the fields are not the same you would take the .setEnabled() method and pass it to be .setEnabled(false); which would cause the button to not be clickable.

 

Hope this helps!!
answered Apr 27, 2015 by Joshua Wielenga (100 points)
+4 votes

You could use 

button.setEnabled(false);

and

button.setEnabled(true);

to disable the button for a time. What I would do is have the submit button check the password fields and if they are equal then do the next step. If they are not equal then toast a message indicating that the passwords do not match and clear the EditText, then do nothing else. The next time they click the submit button this process is repeated.

answered Apr 27, 2015 by Ethan Wojcinski (100 points)
edited Apr 27, 2015 by Ethan Wojcinski
...