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...
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...
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.
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.