Ramesh Kumar Ramesh Kumar - 10 days ago 5
Android Question

Check entered value is number or string in android?

I've tried this snippet, but it doesn't work

try
{
Integer.parseInt(enteredID.getText().toString());
Log.i("enteredID value", "enterdID is numeric!!!!!!!!!!!^^^");
flag=1;
} catch (NumberFormatException e) {
flag=-1;
Log.i("enteredID value", "enterdID isn't numeric!!!!!!!!!!!^^^");
}

Answer

If boolean value is true then it is number otherwise string value

boolean digitsOnly = TextUtils.isDigitsOnly(editText.getText());

for Example

String text = edt_email.getText().toString();
        boolean digitsOnly = TextUtils.isDigitsOnly(text);
        if (digitsOnly) {
             if (text.length() == 0) {
                Toast.makeText(getApplicationContext(), "field can't be empty.", Toast.LENGTH_LONG).show();
             } else {
               Toast.makeText(getApplicationContext(), "field is int value", Toast.LENGTH_LONG).show();
             }
        }else {
                Toast.makeText(getApplicationContext(), "Field is string value", Toast.LENGTH_LONG).show();
            }
        }