Check if string is number in android

If you are inflating those textviews from xml you can just use:

android:inputType="number"

You could use this util method which uses the same regex as used in TextView.

fun isPhoneNumber(number: String): Boolean {
    return if (number.isEmpty()) {
        false
    } else {
        Patterns.PHONE.matcher(number).matches()
    }
}

just note from the Patterns.PHONE docs

This pattern is intended for searching for things that look like they might be phone numbers in arbitrary text, not for validating whether something is in fact a phone number. It will miss many things that are legitimate phone numbers.

The pattern matches the following:

Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes may follow. Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes. A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.


Use string.matches method which accepts a regex as an argument.

if(string.matches("\\d+(?:\\.\\d+)?"))
{
System.out.println("Matches");
}
else
{
System.out.println("No Match");
}

If you are looking for a way to just check if the string contains digits (so check for integer values), use the native TextUtils.isDigitsOnly(CharSequence str).