0

I have to make a webbrowser for android, so I want to try to block a site.

How can I do that?

3 Answers 3

1

Lets say your WebView id is myWebView then what you will do is this :

 WebView wb = (WebView) findViewById(R.id.myWebView); wb.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.contains("http://yourBlockedUrl.com")){ //notify the user that this url is blocked return true; } return false; } }); 

by doing this you are overriding the url loading of your webview you can thus block a url from loading.

Sign up to request clarification or add additional context in comments.

1 Comment

please modify the if statement to your needs , this was a rough example of how to check a url, what url are you loading to the webview?
0

say you get the url sent as a string "www.google.com", from the edittext just do a check if this is a blocked url. for example

if( "www.google.com".equalsIgnoreCase(blocked_string)) { webview.setVisibility(View.GONE); warning_view.setVisibilty(View.VISIBLE); } 

or you could try overriding the shouldOverrideUrlLoading() in the WebViewClient class

@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if(my_url.equals("www.google.com")){ //do something. } return true; } 

Creating and setting a WebViewClient subclass. It will be called when things happen that impact the rendering of the content, eg, errors or form submissions. You can also intercept URL loading here (via shouldOverrideUrlLoading()).

6 Comments

I have problem on warning_view, what should I do?
add a webview to you xml layout. then use findviewbyid() to get it in the activity fragment. you need to add a webview first though. once you have the webview you can make it appear/disappear using webview.setVisibility(). "warning_view" is just an example name for your instance of WebView in my snippet of code .developer.android.com/reference/android/webkit/WebView.html
It's better to block the URL from loading in the Webview itself rather than catch an EditText
you should try debuging the code. put a break point in your shouldOverrideUrlLoading(WebView view, String url) method and see if it it executed
I am a newbie in android, I have debugging but I dont know how I can see it
|
0
public void gotoUrl(View view) { EditText theEditText = (EditText)findViewById(R.id.urlTxt); theUrl = theEditText.getText().toString(); // // String blok= "http://www.teknojurnal.com"; webBrowserKu.loadUrl(theUrl); } 

my web browser when klik go, will process this, so what is the problem? why I cant do the steps from anything for blocking one site?

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.