16

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.

Community
  • 1
  • 1
yesButNotReally
  • 162
  • 1
  • 1
  • 7

10 Answers10

43

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
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Hariharan
  • 24,741
  • 6
  • 50
  • 54
6

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;
}
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
2

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()
Gibolt
  • 42,564
  • 15
  • 187
  • 127
1
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
}
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
Samer Kasseb
  • 192
  • 3
  • 10
  • 3
    Welcome to Stack Overflow. Answering questions here is good but a good answer is more than just code. When answering an old question it is helpful to add significant new information and new insights to the problem. – AdrianHHH May 26 '19 at 08:26
0

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;
}
J.Done
  • 1
  • 1
0

Simply do the following

String s = (EditText) findViewByID(R.id.age)).getText().toString();
TextUtils.isEmpty(s);
AZIM MOHAMAD
  • 72
  • 11
0

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
}
kk.
  • 3,747
  • 12
  • 36
  • 67
Sunil
  • 1
  • 1
    there is a typepo! EditText username = (EditText) findViewById(R.id.editTextPassword) .. should be on a new line nd is not part of the comment – Sunil Jun 12 '19 at 09:47
0

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

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

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();
Tristan Elliott
  • 594
  • 10
  • 8
-1

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;

}
Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55