Question

I have an EditText in android for users to input their AGE. It is set an inputType=phone. I would like to know if there is a way to check if this EditText is null.

I've already looked at this question: Check if EditText is empty. but it does not address the case where inputType=phone.

These, I've checked already and do not work:

(EditText) findViewByID(R.id.age)).getText().toString() == null
(EditText) findViewByID(R.id.age)).getText().toString() == ""
(EditText) findViewByID(R.id.age)).getText().toString().matches("")
(EditText) findViewByID(R.id.age)).getText().toString().equals("")
(EditText) findViewByID(R.id.age)).getText().toString().equals(null)
(EditText) findViewByID(R.id.age)).getText().toString().trim().length() == 0
(EditText) findViewByID(R.id.age)).getText().toString().trim().equals("")
and isEmpty do not check for blank space.

Thank you for your help.

Was it helpful?

Solution

You can check using the TextUtils class like

TextUtils.isEmpty(ed_text);

or you can check like this:

EditText ed = (EditText) findViewById(R.id.age);

String ed_text = ed.getText().toString().trim();

if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
    //EditText is empty
}
else
{
    //EditText is not empty
}

OTHER TIPS

First Method

Use TextUtil library

if(TextUtils.isEmpty(editText.getText().toString()) 
{
    Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
    return;
}

Second Method

private boolean isEmpty(EditText etText) 
{
        return etText.getText().toString().trim().length() == 0;
}

Add Kotlin getter functions

val EditText.empty get() = text.isEmpty() // it == ""
// and/or
val EditText.blank get() = text.isBlank() // it.trim() == ""

With these, you can just use if (edittext.empty) ... or if (edittext.blank) ...

If you don't want to extend this functionality, the original Kotlin is:

edittext.text.isBlank()
// or
edittext.text.isEmpty()
EditText textAge;
textAge = (EditText)findViewByID(R.id.age);
if (TextUtils.isEmpty(textAge))
{
Toast.makeText(this, "Age Edit text is Empty", Toast.LENGTH_SHORT).show();
//or type here the code you want
}

I use this method for same works:

public boolean checkIsNull(EditText... editTexts){
for (EditText editText: editTexts){
  if(editText.getText().length() == 0){
    return true;
  }
}
return false;
}

Simply do the following

String s = (EditText) findViewByID(R.id.age)).getText().toString();
TextUtils.isEmpty(s);

I found that these tests fail if a user enters a space so I test for a missing hint for empty value

EditText username = (EditText) findViewById(R.id.editTextUserName);

EditText password = (EditText) findViewById(R.id.editTextPassword);

// these hint strings reflect the hints attached to the resources

if (username.getHint().equals("Enter your username") || password.getHint().equals("Enter Your Password")){
      // enter your code here 

} else {
      // alls well
}

editText.length() usually works for me try this one

if((EditText) findViewByID(R.id.age)).length()==0){
   //do whatever when the field is null`
}

Java: Check if EditText is Empty

1) find the EditText

 ourEditText = view.findViewById(R.id.edit_text);

2) Get String value of EditText

 String ourEditTextString = ourEditText.getText().toString();

3) Remove all spaces

  • Incase user inputs only blank spaces.
String ourEditTextNoSpaces = ourEditTextString.replaceAll(" ","");

3) Check if empty

boolean isOurEditTextEmpty = ourEditTextNoSpaces.isEmpty();

Try this. This is the only solution I got

String phone;   

 try{
phone=(EditText) findViewByID(R.id.age)).getText().toString();

}
catch(NumberFormatException e){

 Toast.makeText(MainActivity.this, "plz enterphone Number ", Toast.LENGTH_SHORT).show();
    return;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top