-1

I have put two EditText in my Login page. At first I disabled the login button.

I want to enable the login button When user fills all the two fields.

How to do it? Please give an appropriate solution...

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
  • using onfouselistner in android apply on edit text – Naveen Tamrakar Dec 30 '14 at 05:48
  • possible duplicate of [Enable and disable Button according to the text in EditText in Android](http://stackoverflow.com/questions/8225245/enable-and-disable-button-according-to-the-text-in-edittext-in-android) – Himanshu Agarwal Dec 30 '14 at 05:54

3 Answers3

0
button1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String text1 =editText.getText().toString().trim();
                if (text1.length()!=0) {
                  // Filed is not empty do your code here.
                } else {
                    editText.requestFocus();
                    editText.setError("Enter Text");
                    Toast.makeText(getApplicationContext(),"Enter Text",Toast.LENGTH_SHORT).show();
                }
           }
        });

Another way is to addTextChangedListener to your EdiText

Like this:

yourEditText.addTextChangedListener(new TextWatcher() {
      @Override
      public void afterTextChanged(Editable arg0) {
         enableSubmitIfReady();
      }

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

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

In that method, you should do like this:

 public void enableSubmitIfReady() {

    boolean isReady =yourEditText.getText().toString().length()>1;

    if (isReady) {
      yourbutton.setEnabled(true);
   } else {
      yourbutton.setEnabled(false);
    }
  }

Hope it helps.

Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
  • Hey Thanks.... i knew the Onclick Listener. It works when the user clicks a button. But i disabled the button initially. – magdoon basha Dec 30 '14 at 05:49
0

Use TextWatcher in android for both text fields.

Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
0

Use following code

TextWatcher textWatcher = new TextWatcher() {

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

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        validateFields();
    }
};

validateFields() method

protected void validateFields() {

    if(edt1.getText().length()>0 && edt2.getText().length()>0){
        btnLogin.setEnabled(true);
    }else{
        btnLogin.setEnabled(false);
    }
}

Inside validateFields() method, you can check length of input fields. If length are more than 0, you can enable Login button.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93