Home » Android » Android TextUtils.isDigitsOnly returns true for empty string

Android TextUtils.isDigitsOnly returns true for empty string

Posted by: admin June 15, 2020 Leave a comment

Questions:
android.text.TextUtils.isDigitsOnly("")  

I’m using above code to check input string is a valid number or not but isDigitOnly() function return true for empty string.

How to make it to return false for empty string or there is other Android build in function?

How to&Answers:

Do it like this:

boolean myIsDigitsOnly(String str) {
    if(str.isEmpty()) {
        return false;
    } else {
        return TextUtils.isDigitsOnly(str); 
    }
}

Now you call your own custom method:

myIsDigitsOnly(""); // returns false

As Selvin mentioned correctly this is actually a bug that persists until today. See here.