How to test if an Edittext is empty?

Asked

Viewed 10,560 times

4

I would like to know how I do to test whether a Edittext is empty or filled on Android.
For example, I want to make an app that will perform a registration, but there are fields that cannot be empty so, I would like to know how to do this test, in case, if there was an empty field, it would return a message saying that the fields cannot be empty.

7 answers

4


if(meuEditText.getText().length() == 0){//como o tamanho é zero é nulla aresposta

       meuEditText.setError("Campo vazio");

}else if (meuEditText.getText().length() < 5){

      meuEditText.setError("Minimo 5 letras");

}

Next post some code, post your attempt,

  • getText() never returns null.

  • compare so clength() == 0** that vc will leave that has nothing filled, that is null anyway. try to put a meuEditText.gettext(). toString(). If the size is zero is null, nothing is filled

  • Hence yes, then edit your answer.

  • 1

    @Piovezan the guy wants the answer, not put any code, then I’ll help, put something for him to think a little, and you diminish me, will understand. :(

  • 2

    I just asked you to correct your answer, which was incorrect (in the empty Java string is different from null) and I returned the vote in favor of your answer, nothing more. If the person who asked did not provide details, ask for these details in the comments before attempting a reply.

4

if (meuEditText.getText().toString().trim().equals("")) {
    ....

Note that getText() never returns the value null, at worst it returns empty, ie, "".

Optionally you can test length() == 0 or isEmpty() (the latter case from API level 9).

2

You can check this way by creating the following function, which will check if it has white space and if the string size is less than 1.

private boolean isEmpty(EditText etText) {
        String text = etText.getText().toString().trim();
        if (text.length()<1)
            return true;
        return false;
}

I answered this question here also.

  • 1

    I think isEmpty() is the most interesting method. Thanks for the tip!

2

You can test this way, with the equals("")

Ex:

@Override
    public void onClick(View view) {
        if (edtAltura.getText().toString().equals("")) {
            Toast.makeText(getApplicationContext(), "Campo Altura está vazio!", Toast.LENGTH_SHORT).show();
            edtAltura.requestFocus();
        } else if (edtPeso.getText().toString().equals("")) {
            Toast.makeText(getApplicationContext(), "Campo Altura está vazio!", Toast.LENGTH_SHORT).show();
            edtPeso.requestFocus();
        } else {
    }
}

0

if(editText.getText().toString().matches("")){
 .....
}

The method matches() tells whether or not the string passed corresponds to a given regular expression.

  • Put some comments to explain your code. Give the solution without explaining, I do not think it is a good idea

0

The most efficient way is by using the class TextUtils as follows:

TextUtils.isEmpty(nome_editText.getText().toString());

it checks for example if 2 blank spaces have been inserted. What does not happen if you take the size of the string inserted in the editText.

0

You can use the following:

if (meuEditText.getText().toString().isEmpty()) {
   //faça tal...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.