Check entered value is number or not
android, integer
Solution
If boolean value is true then it is number otherwise string value
boolean digitsOnly = TextUtils.isDigitsOnly(editText.getText());
or 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();
}
}
Problem
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!!!!!!!!!!!^^^"); } ``` take care that it can accept either username or id to check the value, I don't want it to accept only numbers!!