0

I want to display toast when fields are empty.

button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
        }

    });
Pritam
  • 339
  • 3
  • 23
DEXTOR_ANTONY
  • 300
  • 1
  • 12
  • 1
    Why you comment all answer with 'Thank you for your response.problem solved'? Just some answer are correct, not all! – Abner Escócio Jul 30 '18 at 12:49
  • @AbnerEscócio Actually this is my first question on this site, and I'm overwhelmed to see these much replies in instant. Will take your advice! – DEXTOR_ANTONY Jul 30 '18 at 13:03
  • 2
    If any of the answers have helped you, select the one that's helped you most by clicking the ✔ icon underneath the score. This will mark the question as "answered", and you and the answerer will receive reputation. – Michael Dodd Jul 30 '18 at 13:03

4 Answers4

3

Before doing that

aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)

Try to check if data is empty as follow

if (edittext1.getText().toString().isEmpty() || edittext2.getText().toString().isEmpty()) {
    Toast.makeText(this,"fill the required field",Toast.LENGTH_LONG).show();
} else {
    aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)
}
Abner Escócio
  • 2,697
  • 2
  • 17
  • 36
Pritam
  • 339
  • 3
  • 23
3

Try this :

When the fields are empty, apply this check :-

if (edittext1.getText().toString().isEmpty()) {

Toast.makeText(getApplicationContext(),"Edit text 1 can not be empty",Toast.LENGTH_LONG).show();

}

else if (edittext2.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Edit text 2 can not be empty",Toast.LENGTH_LONG).show();
   }
    else{
     //success code here
        aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);

    }
Rajshree Tiwari
  • 630
  • 1
  • 7
  • 27
2

Instead of toast, "setError" to edit texts. It would be better

editText.setError("This field can not be blank");
Santhosh Joseph
  • 744
  • 7
  • 18
2

Try this,

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(edittext1.getText().toString().length()<=0){
            Toast.makeText(this, "email is empty", Toast.LENGTH_SHORT).show();
        }else if (edittext2.getText().toString().length()<=0){
            Toast.makeText(this, "password is empty", Toast.LENGTH_SHORT).show();
        }else{
            aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
        }
    }

});
Shubham Vala
  • 1,024
  • 7
  • 18