69

I am new in Android and I am trying to open a Link in webview using this code

WebView myWebView = (WebView) findViewById(R.id.webinfo);
    myWebView.loadUrl("http://oslobokfestival.netteam.no/artical.php?articalid=93");
    myWebView.setBackgroundResource(R.drawable.lbg);
    myWebView.setBackgroundColor(Color.TRANSPARENT);
    myWebView.getSettings().setJavaScriptEnabled(true);

and in this HTML page contains some links and I want that when user click that link should be open in same webview, at this point its opening in mobile browser, please give me appropriate solution.. Thanks.

Mahek
  • 803
  • 1
  • 8
  • 13

6 Answers6

169

You need to add WebViewClient to your WebView in order to open it in the WebView. Something like

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return false;
    }
});

Tim
  • 41,901
  • 18
  • 127
  • 145
momo
  • 21,233
  • 8
  • 39
  • 38
  • this works, but is there a way to allow links that have target=_blank applied on them to be opened in new window?? – mim Nov 18 '14 at 12:13
  • Its working fine but I want to add an icon on external webview – Reaching-Out Apr 21 '16 at 07:35
  • 6
    `shouldOverrideUrlLoading(WebView view, String url)` is deprecated and we can use `shouldOverrideUrlLoading(WebView view, WebResourceRequest request)` instead. – Capella Oct 07 '16 at 08:26
  • getUrl() requires API level 21, So you couldn't run app on some devices. – QMaster Nov 02 '18 at 21:10
  • 6
    The documentation says you should not call `view.loadUrl(...)` and instead just return false: "Note: Do not call WebView#loadUrl(String) with the request's URL and then return true. This unnecessarily cancels the current load and starts a new load with the same URL. The correct way to continue loading a given URL is to simply return false, without calling WebView#loadUrl(String)." (https://developer.android.com/reference/android/webkit/WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView,%20android.webkit.WebResourceRequest)) – Lorenz Mar 25 '20 at 21:57
14

Try remove @Overrideand an put it after loadurl This work for me...

myWebView.loadUrl("http://someurl.com");
myWebView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
        viewx.loadUrl(urlx);
        return false;
    }
});
Todd
  • 30,472
  • 11
  • 81
  • 89
wsewed
  • 182
  • 1
  • 4
  • 3
    @Overrideand is just annotation to tell your overriding a method, you can (and you should) keep it in there – Srneczek Jan 12 '16 at 19:11
5

For me its works by simply overriding

shouldOverrideUrlLoading methods and

return super.shouldOverrideUrlLoading(view, request)

that will handle all links in same WebView.

webview.setWebViewClient(new WebViewClient(){
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
         return super.shouldOverrideUrlLoading(view, request);
      }
});
Bhavesh Desai
  • 753
  • 4
  • 20
1

Override the method shouldOverrideUrlLoading of WebViewClient like this:

myWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.toString());
            return true;
        }
    });

and add this tag <uses-permission android:name="android.permission.INTERNET" /> in your manifest file To get access to the internet

Melis
  • 33
  • 8
1
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.loadUrl("#");
    }
}
Nikola Lukic
  • 4,001
  • 6
  • 44
  • 75
1

Here is how to do it kotlin and also take care of the Versions below 21:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            myWebView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView,
                    request: WebResourceRequest
                ): Boolean {
                    view.loadUrl(request.url.toString())
                    return false
                }
            }
        } else {
            myWebView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    view?.loadUrl(url.toString())
                    return false
                }
            }
        }
Amin Keshavarzian
  • 3,646
  • 1
  • 37
  • 38