neena
neena

Reputation: 581

How to disable button in android?

I have a login page,having 2 textfields username,password and button "login" button. when the username or password not entered alert is poping up, when i suddenly clicks on this login button 2 alerts are poping up, since more than click is taken. I need to disable the button and enable during next click. What should i do?

Upvotes: 0

Views: 251

Answers (3)

Siddharth Lele
Siddharth Lele

Reputation: 27748

You can combine blackbelt's solution with mine.

Add a TextWatcher to your EditText and enable or disable the button when text is entered in the EditText.

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    charCount = Integer.valueOf(s.length());

    if (charCount > 0)  {           
        // ENABLE THE BUTTON
        boolean blnButtonStatus = YOUR_BUTTON.isEnabled();
        if (blnButtonStatus== false)    {
            YOUR_BUTTON.setEnabled(true);
        }

    } else if (charCount == 0)  {
        // DISABLE THE BUTTON
        YOUR_BUTTON.setEnabled(false);
    }
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // DO NOTHING HERE
}

@Override
public void afterTextChanged(Editable s) {
    // DO NOTHING HERE
}

This way, the code will automatically disable the Button if there is nothing typed in the Password and /or Username fields. No accidental clicks.

Upvotes: 0

Kosh
Kosh

Reputation: 6334

create a method as below

    boolean checkit= true;
private void showHide1() {

    if (checkit) {

    // first click
    } else {
    // second click
    }
    checkit= !checkit;
}

and on you button click add the method showHide1();

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

in order to disable a button, you have to call

yourButton.setEnabled(false)

on a Button instance. To re enable it call:

yourButton.setEnabled(true)

Upvotes: 1

Related Questions