Add http://www. in the text if not Exist

You can do like this

String url = textView.getText().toString();
if(!url.startsWith("www.")&& !url.startsWith("http://")){
  url = "www."+url;
}
if(!url.startsWith("http://")){
  url = "http://"+url;
}

You can use this url to display content in WebView

Hope this will solve your problem


The most efficient way of checking that the domain name is well formed and contains (or not) a prefix, is using a regular expression.

Check out Java Pattern to match regex in Android. It is worth it.


just modified @silwar answer and add https :

 if(!url.startsWith("www.")&& !url.startsWith("http://") && !url.startsWith("https://")){
        url = "www."+url;
    }
    if(!url.startsWith("http://") && !url.startsWith("https://")){
        url = "http://"+url;
    }

But remember it that sometimes http:// create security exception in android so we should use https://.So for risk-free code, we have to do that like the last checking -

 if(!url.startsWith("http://") && !url.startsWith("https://")){
            url = "https://"+url;}