32

I have a webView in Android, and I open a html webpage in it. But it's full of links and images, and when I click one of them, it loads in my webview. I want to disable this behaviour, so if I click on a link, don't load it. I've tried this solution and edited a bit for myselft, but not worked. My webviewclient code:

private boolean loaded = false; @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if(loaded == false){ view.loadUrl(url); loaded = true; return true; }else{ return false; } } 

My webview implementation and settings.

WebView wv = (WebView) findViewById(R.id.recipeWv); ourWebViewClient webViewClient = new ourWebViewClient(); webViewClient.shouldOverrideUrlLoading(wv, URLsave); wv.setWebViewClient(webViewClient); wv.setFocusableInTouchMode(false); wv.setFocusable(false); wv.setClickable(false); WebSettings settings = wv.getSettings(); settings.setDefaultTextEncodingName("utf-8"); settings.setLoadWithOverviewMode(true); settings.setBuiltInZoomControls(true); 

Example: If the user open in my webview the StackOverflow homepage and clicks on one of the links(like "Questions") then the webview should stay on the StackOverflow homepage.

8 Answers 8

39

You should save in a class variable your current url and check if it's the same with the loaded one. When the user clicks on a link the shouldOverrideUrlLoading is called and check the website. Something like this:

private String currentUrl; public ourWebViewClient(String currentUrl) { this.currentUrl = currentUrl; } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.equals(currentUrl)) { view.loadUrl(url); } return true; } 

Important: don't forget set the WebViewClient to your WebView.

ourWebViewClient webViewClient = new ourWebViewClient(urlToLoad); wv.setWebViewClient(webViewClient); 
Sign up to request clarification or add additional context in comments.

1 Comment

can we disable clicks somehow? menu is still expandable within webview which shows links to navigate but of course can't navigate after this solution.
20

Implement a WebViewClient and Just return true from this method of WebView Client

webView.setWebViewClient(new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { return true; } }); 

1 Comment

most correct way of stopping/preventing webview from opening any url contained in the content. **** --- Its Just for stopping-- ****
5

If you want to open inner link of a web page in different window then

Don't use

WebView webView;//Your WebView Object webView.setWebViewClient(new HelpClient());// Comment this line 

B'coz setWebViewClient() method is taking care of opening a new page in the same webview. So simple comment this line.

private class HelpClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("www.example.com")) { // This is my web site, so do not override; let my WebView load the page return false; } // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); return true; } } 

Hope this will work.

3 Comments

I don't want to navigate to other pages, stay on the same. So like disable the references and links.
@Dandelion you are loading static html page from assets or it is an URL?
It's an URL. I have lot of references in a ListView, and when the user click one of them, it's openning in a webview.
3

Even I had been facing the same issue. I solved this issue like below

webView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); 

With lambda expressions

webView.setOnTouchListener((v, event) -> true); 

1 Comment

This prevents the user from being able to scroll so it only works if you don't need to scroll inside your WebView
3

Note that the method signature changed in API 24.

For APIs earlier than 24 the second parameter is String and that signature is now deprecated.

For APIs 24 and later, the second parameter is WebResourceRequest.

If your app supports both pre and post API 24 and you want to disable all links you can use this:

 webView.setWebViewClient(new WebViewClient(){ @Override //for APIs 24 and later public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){ return true; } @Override //for APIs earlier than 24 public boolean shouldOverrideUrlLoading(WebView view, String url){ return true; } }); 

Comments

2

Using inject javascript On @Override onPageFinished

view.loadUrl("javascript: (function () {document.addEventListener('click', function (e) {e.stopPropagation();}, true);})()"); 

1 Comment

Please edit your answer to ensure that it improves upon other answers already present in this question.
1

You need to add an extra line of code, like so-

webview.loadUrl(url); webview.setWebViewClient(new MyWebViewClient()); 

And add an additional class MyWebViewClient like this-

/* Class for webview client */ class MyWebViewClient extends WebViewClient { // show the web page in webview but not in web browser @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onLoadResource(WebView view, String url) { super.onLoadResource(view, url); } } 

2 Comments

Yes I use it, I will update my question with the implementation.
I've updated my answer. This seems to be working well for me. Hope it helps.
0

Very similar to Amit Gupta's answer, but I found a shorter way to do it.

private String currentUrl; public CustomWebViewClient(String currentUrl) { this.currentUrl = currentUrl; } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return ! url.equals(currentUrl); } 

After this CustomWebViewClient is defined, set the client to the webview.

CustomWebViewClient webViewClient = new CustomWebViewClient(urlToLoad); wv.setWebViewClient(webViewClient); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.