0

I have this code and it works fine to open a web page :

package com.example.fragmenttabs;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.app.Fragment;

public class FragmentTab2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragmenttab2, container, false);

        String url = "http://example.com";
        WebView view = (WebView)rootView.findViewById(R.id.webView2);        
        view.getSettings().setJavaScriptEnabled(true);          
        view.loadUrl(url);

        return rootView;
    }

}

but unfortunately, when I clicked on a link inside that webview, it will open Google Chrome. How to disable this behaviour? I want to make it open in same webview. not open in Google Chrome. thank you.

user4232
  • 582
  • 1
  • 12
  • 37
Saint Robson
  • 5,211
  • 15
  • 63
  • 106
  • possible duplicate of [Android webview opens page in default browser instead of my webview](http://stackoverflow.com/questions/24035307/android-webview-opens-page-in-default-browser-instead-of-my-webview) –  Sep 18 '15 at 13:58

3 Answers3

10

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
4

To open webivew link inside webview use setWebViewClient as

webView.setWebViewClient(new WebViewClient() {

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

As stated in document,It Return true if the host application wants to leave the current WebView and handle the url itself, otherwise return false.

For more info see at Webview shouldOverrideUrlLoading

Giru Bhai
  • 14,097
  • 5
  • 43
  • 70
2

Implement WebViewClient() like:

 WebView _open;
 WebSettings webSettings = _open.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        _open.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }

        });
        _openpaypal.loadUrl(url);
Pankaj Arora
  • 10,731
  • 2
  • 37
  • 62