1

Heres the acitivity of my code I want to like to block external link like if someone click on another webpage he or she cannot go to that webpage.

public class NewsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news); String url = "https://www.example.com"; WebView web = (WebView) findViewById(R.id.webView4); web.loadUrl(url); final WebView mWebView = (WebView) findViewById(R.id.webView4); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); mWebView.getSettings().setSavePassword(true); mWebView.getSettings().setSupportZoom(true); mWebView.getSettings().setSaveFormData(true); mWebView.getSettings().setSupportZoom(false); mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); mWebView.getSettings().setDomStorageEnabled(true); mWebView.getSettings().setSupportMultipleWindows(false); mWebView.getSettings().setLightTouchEnabled(true); mWebView.getSettings().setBuiltInZoomControls(true); mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); mWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { mWebView.loadUrl("javascript:(function() { " + "document.getElementsByClassName('header_wrapper')[0].style.display='none'; " + "document.getElementsByClassName('footer-contact')[0].style.display='none'; " + "document.getElementsByClassName('navbar-header')[0].style.display='none'; " + "document.getElementsByClassName('footer-social')[0].style.display='none'; " + "document.getElementById('footer_bottom').style.display='none'; " + "document.getElementById('footer_content').style.display='none'; " + "document.getElementById('core_mobile_menu').style.display='none'; " + "document.getElementById('catapult-cookie-bar').style.display='none'; " + "})()"); } }); mWebView.loadUrl("https://www.example.com"); } } 
4
  • Where should I add this line of code? Or is it possible if you can show me where you added it thanks Commented Dec 5, 2017 at 6:03
  • Its already mention in your question you have already set the WebViewClient just override one more method which is shouldOverrideUrlLoading() . And you are good to go . Commented Dec 5, 2017 at 6:06
  • If I want to add notification how can I do it ? Commented Dec 7, 2017 at 5:44
  • add notification ?? Where? Explain . or post a new question cause it seems totally different. Commented Dec 7, 2017 at 5:47

3 Answers 3

1

Try this as per your code

 public class NewsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news); String url = "https://www.example.com"; WebView web = (WebView) findViewById(R.id.webView4); web.loadUrl(url); final WebView mWebView = (WebView) findViewById(R.id.webView4); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); mWebView.getSettings().setSavePassword(true); mWebView.getSettings().setSupportZoom(true); mWebView.getSettings().setSaveFormData(true); mWebView.getSettings().setSupportZoom(false); mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); mWebView.getSettings().setDomStorageEnabled(true); mWebView.getSettings().setSupportMultipleWindows(false); mWebView.getSettings().setLightTouchEnabled(true); mWebView.getSettings().setBuiltInZoomControls(true); mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading (WebView view, String url){ return true; } }); mWebView.loadUrl("https://www.example.com"); } } 
Sign up to request clarification or add additional context in comments.

4 Comments

If I want to add notification how can I add in?
Yes you can use JavaScriptInterface in Java file and call this function in your particular webpage
What would be the code ? Can you show me please I'm new to this
please check my given answer stackoverflow.com/questions/47686798/…
0

Try this code this will also handle shouldOverrideUrlLoading deprecation in Nougat and above

mWebview.setWebViewClient(new WebViewClent() { @SuppressWarnings("deprecation") @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("http://www.example.com")) { // This is my web site, so do not override; let my WebView load the page return false; } // reject anything other by returning true return true; } @RequiresApi(Build.VERSION_CODES.N) @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { String url=request.getUrl().toString(); if (Uri.parse(url).getHost().equals("http://www.example.com")) { // This is my web site, so do not override; let my WebView load the page return false; } // reject anything other return true; } } 

Comments

0
 webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading (WebView view, String url){ //True if the host application wants to leave the current WebView and // handle the url itself, otherwise return false. return true; } }); 

Use Like this :

 WebView webview = new WebView(context); webview.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); Log.d("WebView", "your current url when webpage loading.." + url); } @Override public void onPageFinished(WebView view, String url) { Log.d("WebView", "your current url when webpage loading.. finish" + url); super.onPageFinished(view, url); } @Override public void onLoadResource(WebView view, String url) { // TODO Auto-generated method stub super.onLoadResource(view, url); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { System.out.println("when you click on any interlink on webview that time you got url :-" + url); return super.shouldOverrideUrlLoading(view, url); } }); 

1 Comment

Where should I add this line of code? Or is it possible if you can show me where you added it thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.