Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Check if EditText is Empty when inputType is set on Number/Phone

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.

like image 345
yesButNotReally Avatar asked Dec 03 '13 11:12

yesButNotReally


2 Answers

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
}
like image 53
Hariharan Avatar answered Sep 18 '22 02:09

Hariharan


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;
}
like image 27
Zar E Ahmer Avatar answered Sep 21 '22 02:09

Zar E Ahmer