Lev Leontev
Lev Leontev

Reputation: 2615

Load URL in WebView without http://www prefix

Please, don't mark this as duplicate.

If you call WebView.loadUrl with a parameter like google.com, it will fail. So, as other similar questions suggest, you do something like this:

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

But, in case of, say, play.google.com, it will try to load http://www.play.google.com and fail.

If you don't add www, some websites will still fail, for example, eurobeat-prime.com won't work without www prefix.

How can I process such links? (Because modern browsers do)

Upvotes: 1

Views: 1583

Answers (2)

Michael Dougan
Michael Dougan

Reputation: 1698

if you run across a URL that won't open in the webView directly, you can usually succeed in opening it in an external browser window:

protected void handleExternalDeviceActivity(String url) {
    //pass this special URL to an external activity (outside of the app)
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
}

Upvotes: 0

Deˣ
Deˣ

Reputation: 4371

I don't think you need to append www

You can directly use this instead.

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

Upvotes: 2

Related Questions