0

I have a WebView inside a fragment and for example when a user opens that part of my application with the WebView I will show him a website for example ebay or amazon. My problem is that when he clicks on another link from that WebView the new link will open in the default browser. Is there a way to continue opening the links in my WebView?

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
CipQuestion
  • 391
  • 4
  • 11
  • Bear in mind security issues when opening an external website inside a WebView in your app (refer http://developer.android.com/training/articles/security-tips.html#WebView) – FreewheelNat Oct 22 '14 at 09:28

3 Answers3

1

For that you need to set your webclient to the webview explicitly like this

webview.setWebViewClient(new WebViewClient());
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
1

You have to define a custom WebViewClient to your WebView to handle this.

The question was already answered here and here.

Community
  • 1
  • 1
mithrop
  • 3,283
  • 2
  • 21
  • 40
1

Use WebViewClient for that

myWebView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub

        if(url.contains("amazon"))  //compare urls that should open in same webview
        {
            return super.shouldOverrideUrlLoading(view, url);
        }
        else
        {    
            //open in default browser
            view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }
    }
});

Hope it helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124