How to check if URL is valid in Android How to check if URL is valid in Android android android

How to check if URL is valid in Android


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.


URLUtil.isValidUrl(url);

If this doesn't work you can use:

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


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;}