3

I followed this tutorial: http://examples.javacodegeeks.com/android/core/ui/webview/android-webview-example/

Every time I click on a link in the webview, it takes me out of the webview and into a separate browser window. How do I get all the links to open in the webview?

Katie H
  • 2,215
  • 5
  • 26
  • 47

1 Answers1

8

You need to implement setWebViewClient(....) like so.

webView.setWebViewClient(new WebViewClient() {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
        }
});
M D
  • 46,860
  • 8
  • 87
  • 108
  • I am getting the error, cannot resolve symbol WebViewClient – Katie H Jan 06 '15 at 13:39
  • 2
    @KatieH for that you should `import android.webkit.WebViewClient;` – M D Jan 06 '15 at 13:41
  • Thank you sooooooo much!!!!!! :))) One quick question, if you take a look at the tutorial, it requires the user to click a button to load the webview, do I have to do this? Can I make the webview load automatically with having to click the button? How can I do that? – Katie H Jan 06 '15 at 13:49
  • 1
    Should I ask this in a separate stackoverflow question? – Katie H Jan 06 '15 at 13:52
  • @KatieH Off course you can do it without any `Button click` just move all button `onClick(..)` code to `Activity onCreate(...)`. – M D Jan 07 '15 at 04:40
  • before loadurl try this: myWebView.setWebViewClient(new WebViewClient()); – UMAR-MOBITSOLUTIONS Dec 22 '15 at 14:15