Thursday, October 13, 2011

Android: Validate a URL string

Sometimes all we really need is to check if a URL is valid. You know, the standard "http(s)://domain.com/something/" pattern.

It took a while to find, but the API level 1 had it all along!

Just use URLUtil.isValidUrl() to test it.

// Validate URL
if (!URLUtil.isValidUrl(url)) {
Toast.makeText(this, "Invalid URL specified", Toast.LENGTH_SHORT).show();
return;
}

That's all folks!

 slidedog
Back to having fun!

Sources

0 comments:

Post a Comment

Leave your thoughts ...

---
If you are having trouble with copy/pasting in comments, you need to sign in or click 'Preview'.

For more information about this Firefox bug, see here.