How to determine if an input in EditText is an integer?
Update:
You can control the EditText to only accept numbers
<TextView
.
.
.
android:inputType="number"
/>
or check it programmatically
In Kotlin
val number = editText123.text.toString().toIntOrNull()
val isInteger = number != null
In Java
String text = editText123.getText().toString();
try {
int num = Integer.parseInt(text);
Log.i("",num+" is a number");
} catch (NumberFormatException e) {
Log.i("",text+" is not a number");
}
Since API level 1, Android provides an helper method to do just that (no need to use regex or catching exception) : TextUtils.isDigitsOnly(CharSequence str)
boolean digitsOnly = TextUtils.isDigitsOnly(editText.getText());
Note that this method returns true with empty String : Issue 24965