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.
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
}
First Method
Use TextUtil library
Second Method
You can check using the TextUtils class like
or you can check like this:
I found that these tests fail if a user enters a space so I test for a missing hint for empty value
Add Kotlin getter functions
With these, you can just use
if (edittext.empty) ...
orif (edittext.blank) ...
If you don't want to extend this functionality, the original Kotlin is:
I use this method for same works: