How to check if URL is valid in Android

Solution 1:

Use URLUtil to validate the URL as below.

 URLUtil.isValidUrl(url)

It will return True if URL is valid and false if URL is invalid.

Solution 2:

URLUtil.isValidUrl(url);

If this doesn't work you can use:

Patterns.WEB_URL.matcher(url).matches();

Solution 3:

I would use a combination of methods mentioned here and in other Stackoverflow threads:

public static boolean IsValidUrl(String urlString) {
    try {
        URL url = new URL(urlString);
        return URLUtil.isValidUrl(urlString) && Patterns.WEB_URL.matcher(urlString).matches();
    } catch (MalformedURLException ignored) {
    }
    return false;
}