Home » Android » Register isEmpty

Register isEmpty

Posted by: admin November 1, 2017 Leave a comment

Questions:
//GET TEXTS //
    final String firstName= editText_firstName.getText().toString();
    final String lastName = editText_lastName.getText().toString();
    final String email = editText_email.getText().toString();
    //final String password = editText_password.getText().toString();
    //final String dateOfBirth = editText_dateOfBirth.getText().toString();
    //final String idNumber = editText_idNumber.getText().toString();



    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           if(firstName.isEmpty() || firstName.length() == 0 || firstName.equals("") || firstName == null){


               Toast.makeText(MainActivity.this, "Fill the name", Toast.LENGTH_SHORT).show();
           }
           else{

               Toast.makeText(MainActivity.this, "Successful", Toast.LENGTH_SHORT).show();
           }
        }
    });

Why toast result is always “Fill the name” even if I filled that blank?
Output never gives “successful” result

Answers:

You have to get the value of editText_firstName.getText().toString(); inside your onClick() listener

Questions:
Answers:

Because you are setting the firstName when the activity loads and the textfield is empty at that time. You should call String firstName= editText_firstName.getText().toString(); at the start of the buttons on click.