In Android, how to make Login button disable with respect to EditText?
You need to implement TextWatcher on EditText
to achieve the result.
EditText et1, et2;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
b = (Button) findViewById(R.id.button1);
checkValidation();
et1.addTextChangedListener(mWatcher);
et2.addTextChangedListener(mWatcher);
}
private void checkValidation() {
// TODO Auto-generated method stub
if ((TextUtils.isEmpty(et1.getText()))
|| (TextUtils.isEmpty(et2.getText())))
b.setEnabled(false);
else
b.setEnabled(true);
}
TextWatcher mWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
checkValidation();
}
@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
}
};
try this:
EditText et1,et2;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_check);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
b = (Button) findViewById(R.id.button1);
et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
if(s1.equals("") && s2.equals("")){
b.setEnabled(false);
}
else if(!s1.equals("")&&s2.equals("")){
b.setEnabled(false);
}
else if(!s2.equals("")&&s1.equals(""){
b.setEnabled(false);
}
else {
b.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
}
});
et2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
if(s1.equals("") && s2.equals("")){
b.setEnabled(false);
}
else if(!s1.equals("")&&s2.equals("")){
b.setEnabled(false);
}
else if(!s2.equals("")&&s1.equals(""){
b.setEnabled(false);
}
else {
b.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
}
});
}
You need to track user's action inside EditText
using TextWatcher
object:
myEditText.addTextChangedListener(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)
{
if (s.length() > 1)
{
//enable button
} else
//disable
}
});
heres what you are looking for :
private EditText et1,et2;
// create a textWatcher member
private TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void afterTextChanged(Editable editable) {
// check Fields For Empty Values
checkFieldsForEmptyValues();
}
};
void checkFieldsForEmptyValues(){
Button b = (Button) findViewById(R.id.button1);
String s1 = et1.getText().toString();
String s2 = et2.getText().toString();
if(s1.equals("")|| s2.equals("")){
b.setEnabled(false);
} else {
b.setEnabled(true);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_check);
et1 = (EditText) findViewById(R.id.editText1);
et2 = (EditText) findViewById(R.id.editText2);
// set listeners
et1.addTextChangedListener(mTextWatcher);
et2.addTextChangedListener(mTextWatcher);
// run once to disable if empty
checkFieldsForEmptyValues();
}