0

I want to add news and weather app in webview. But jumping to anyther website from webview is not an option. How can I restrict a webview for a certain website.

Thanks.

user1143989
  • 163
  • 1
  • 3
  • 18
  • I don´t understand your question – Sebastian Breit Jun 25 '12 at 14:14
  • Ok. Let me try to explain it. If I put www.weather.com in a webview and user clicks on some advertisement or try to share some thing via facebook or twitter. I should not allow. All the time the basic url has to be www.weather.com. User can browse anything within that url. Does this help ? – user1143989 Jun 25 '12 at 14:18

3 Answers3

2

Create your own WebViewClient:

public class CustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.getHost().equals("weather.com")){
            // load link
            return false;
        }else{
            // block link
            return true;
        }
    }
}

Use it like this:

webview.setWebViewClient(new CustomWebViewClient());

EDIT Note: an example of a getHost() function

Community
  • 1
  • 1
D-32
  • 3,245
  • 2
  • 22
  • 33
0

If I understand the question correctly you might want to implement your own WebViewClient. That will allow you to handle the page navigation. Check out the link for a tutorial from Google.

MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
-1
viewer.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        //Block all URL accesses.
        return false;
    }
});

That should prevent access to anything. You need to handle specific URL's in this function if you want to allow them.

draksia
  • 2,371
  • 2
  • 20
  • 25