EVX
EVX

Reputation: 312

Restricting WebView to one page only

Is it possible to restrict WebViews so that when you have let's say one webview for website but you just want the view to stick to that page and only that page?

Great example is I have a twitter WebView that is following a tag. I just want the user to be able to follow that tag not be able to go deeper into twitter stuff.

It's already a pre-set link,

Button--> opens web view with set link to twitter.

So has to be some kind of check for on press or something like I think.

Upvotes: 4

Views: 4304

Answers (2)

FoamyGuy
FoamyGuy

Reputation: 46856

something like this should do it.

myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient() {
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.contains("somedomain.com")) {
      view.loadUrl(url);
    }
    return true;
  }
});

Upvotes: 4

kumar
kumar

Reputation: 691

In-fact the above is a good solution but if you want an elaborate one. Try this one. I don't know whether this is proper solution for your query. play around with this.

webview.setWebViewClient(new MyWebViewClient());




private class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

             return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);            
            if(!url.equals("your_domain_name.com/")){
                setResult(RESULT_OK);
                finish();
            }
          }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if(!url.equals("your_domain_name.com/")){
                finish();
            }


        }
        @Override
        public void onTooManyRedirects(WebView view, Message cancelMsg,
                Message continueMsg) {
            super.onTooManyRedirects(view, cancelMsg, continueMsg);
            cancelMsg.sendToTarget();
        }

    }

Upvotes: 1

Related Questions