Home » Android » Android WebView click open within WebView not a default browser

Android WebView click open within WebView not a default browser

Posted by: admin April 23, 2020 Leave a comment

Questions:

I did one sample application using WebView, in that web view the URL comes from web services. It’s working fine, but if I click any link within that WebView, its automatically go for default web browser. But I want to open within my application web view only. Here my code:

WvBikeSite = (WebView) findViewById(R.id.wv_bikeWebsite);
wvBikeSite.loadUrl(getBundle.getString("www"));
How to&Answers:

You have to set up a webViewClient for your webView.

Sample:

this.mWebView.setWebViewClient(new WebViewClient(){

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
      view.loadUrl(url);
      return true;
    }
});

Answer:

You need to set up a WebViewClient in order to override that behavior (opening links using the web browser). You obviously have your WebView declared, but then set up a WebViewClient like so:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());

Then you need to define your WebViewClient():

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // Designate Urls that you want to load in WebView still.
            return false;
        }

        // Otherwise, give the default behavior (open in browser)
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}

Then start your WebViewClient:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());

http://developer.android.com/guide/webapps/webview.html

Answer:

I face same problem and i just fixed it by adding single line.

webview.setWebViewClient(new WebViewClient());

and this solved my problem.

Answer:

You need to call wvBikeSite.setWebViewClient, e.g:

    MyWebViewClient wvc = new MyWebViewClient();
    wvBikeSite.setWebViewClient(wvc);

Where MyWebViewClient overrides shouldOverrideUrlLoading, viz:

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

Answer:

You can always open url within webview by using this:

      @Override
  public void onCreate(Bundle savedInstanceState) {
     webview.setWebViewClient(new MyWebViewClient());
 webview.getSettings().setJavaScriptEnabled(true);
 webview.getSettings().setPluginsEnabled(true);
     }


     private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {


            view.loadUrl(url);
            return true;
        }

        @Override
        public void onLoadResource(WebView  view, String  url){

        }
    }    

Answer:

I did like this and its working perfect..

mWebView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null && url.startsWith("http://")) {
                mWebView.loadUrl(url);
                return true;
            } else {
                return false;
            }
        }
    });

Answer:

webview.setWebViewClient(new WebViewClient());
webview.loadUrl(url);

Do set webview client.

Answer:

Try this one, that method is deprecated.

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            return super.shouldOverrideUrlLoading(view, request);
        }

Answer:

Use this code:

// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());       

Answer:

Here is my code how to resolve the above problem (when cliking on link it asking for default browser to opn the link)

import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;
import android.view.Menu;import android.webkit.WebChromeClient;
import android.webkit.WebView;public class MainActivity extends Activity{
`@SuppressLint("SetJavaScriptEnabled")@Override protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String url="http://google.com";WebView Webview=(WebView) this.findViewById(R.id.webView1);     Webview.getSettings().setJavaScriptEnabled(true);Webview.loadUrl(url);}@Override    public boolean onCreateOptionsMenu(Menu menu){//Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu);return true;}`}

Answer:

This Will open any particular link only in your app …

WebView web;

web = (WebView) findViewById(R.id.web);
web.setWebViewClient(new WebViewClient(){

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url != null && url.startsWith("http://www.xplorerstrick.net")) {
            view.loadUrl(url);
            progDailog.show();
            return true;
        }
        else {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}

Answer:

Most of the answer is right but please notice that: SupportMultipleWindows must be set as false.

mWebView.getSettings().setSupportMultipleWindows(false);

and now set webViewClint and get loading URL.

mWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){

    Log.d(TAG,"URL: "+url);
    view.loadUrl(url);

    /**
     * if you wanna open outside of app
     if (url != null && url.startsWith(URL)) {
     view.loadUrl(url);
     return false;
     }
     // Otherwise, give the default behavior (open in browser)
     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
     startActivity(intent);**/
    return true;

}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
}

@Override
public void onLoadResource(WebView view, String url) {
    super.onLoadResource(view, url);
}

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    }
});