0

I have 2 Edittext and a button I don't want the user to be able to click the button if one of the EditText is empty

and I would like to know how to let the button change color to transparent while clicking the button so it looks like you pushed the button.

halfer
  • 19,824
  • 17
  • 99
  • 186
smaika
  • 65
  • 10

6 Answers6

2

You can check this condition:

if(edt_text.getText().equals(""))
{
      yourbutton.setEnabled(false);
      //or
      yourbutton.setClickable(false);   
}
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
1

Try This:-

if(TextUtils.isEmpty(mEdittext.getText()))
{
   button.setEnabled(false);
}

TextUtils is Class From Android Itself.

Amrut
  • 2,655
  • 3
  • 24
  • 44
1
how to make the button unClickable if EditText is empty

As other answers said :

if(edt_text.getText().equals(""))
{
      button.setEnabled(false);
}

I have 2 Edit text and a button I don't want the user to be able to click the button if one of the EditText is empty

In this case it would be better to implement addTextChangedListener in order to setEnable(false) the button if EditText is empty, and to true if not.

editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
            if(s.toString().equals("")) {
                button.setEnabled(false);
            } else {
                button.setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

and I would like to know how to let the button change color to transparent while clicking the button so it looks like you pushed the button

If you are using the default Button style, so you have nothing more to do. But If you define a selector drawable for your button, setEnable() method on the EditText will call the corresponding drawable of android:state_enabled attribute of your selector drawable (if defined).

More info on Selectors here : StateList Drawables

S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
0
if(editText.getText().toString().equalsIgnoreCase(" ") || editText.getText() == null) {

    button.setEnable(false);

}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
0

Answer is short, you have to use:

button.setEnable(false);
Rajendra arora
  • 2,186
  • 1
  • 16
  • 23
0

Code that might Help..

boolean check;
    txt1.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) 
        {
            if(!arg1)
            {
                if(!txt1.getText().toString().trim().equals(""))
                {
                    check=true;
                }
                else
                {
                    check=false;
                }
            }
        }
    });
    txt2.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) 
        {
            if(!arg1)
            {
                if(!txt2.getText().toString().trim().equals(""))
                {
                    check=true;
                }
                else
                {
                    check=false;
                }
            }
        }
    });
    if(check)
    {
        btnn.setEnabled(true);
    }
Shabbir Dhangot
  • 8,954
  • 10
  • 58
  • 80