Clicking URLs opens default browser

前端 未结 6 534
独厮守ぢ
独厮守ぢ 2020-11-22 13:09

I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser

6条回答
  •  我在风中等你
    2020-11-22 13:28

    Arulx Z's answer was exactly what I was looking for.

    I'm writing an app with Navigation Drawer with recyclerview and webviews, for keeping the web browsing inside the app regardless of hyperlinks clicked (thus not launching the external web browser). For that it will suffice to put the following 2 lines of code:

    mWebView.setWebChromeClient(new WebChromeClient()); mWebView.setWebViewClient(new WebViewClient());

    exactly under your WebView statement.

    Here's a example of my implemented WebView code:

    public class WebView1 extends AppCompatActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        WebView wv = (WebView) findViewById(R.id.wv1); //webview statement
        wv.setWebViewClient(new WebViewClient());    //the lines of code added
        wv.setWebChromeClient(new WebChromeClient()); //same as above
    
        wv.loadUrl("http://www.google.com");
    }}
    

    this way, every link clicked in the website will load inside your WebView. (Using Android Studio 1.2.2 with all SDK's updated)

提交回复
热议问题