I want to prevent WebView from loading images except certain image that starts with specific URL.
How to?
Thank you.
This is what you need to do -
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.contains("dummyURL"){ // put a string that is common in urls that you want to load
}else {
view.stopLoading();
}
return false;
}
});
Related
I have to make a webbrowser for android, so I want to try to block a site.
How can I do that?
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.
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()).
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?
After I've filtered out the extension inside my webview, ".png". How can I then use the entire link to open a new activity?
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".png")) {
Toast.makeText(getActivity(), "png clicked", Toast.LENGTH_SHORT).show();
Instead of having my toast, I want to load the URL that was clicked. I'm new to programming in general so I'm not sure the approach.
I would really like to know the use case, but assuming that what you want is just to load that PNG in the webview, you could do this:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains(".png")) {
view.loadUrl(url);
}
return true;
}
Kind regards
Sometimes, when I load my webview with loadUrl, the website is not showing up until I touch the screen or scroll.
It's like I have a webview drawing problem.
Context context = ctx;
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.website);
WebView webView = (WebView) dialog.findViewById(R.id.weburl);
webView.setScrollbarFadingEnabled(false);
//Disable the horizontal scroll bar
webView.setHorizontalScrollBarEnabled(false);
//Enable JavaScript
webView.getSettings().setJavaScriptEnabled(true);
//Set the user agent
webView.getSettings().setUserAgentString("AndroidWebView");
//Clear the cache
webView.clearCache(true);
webView.loadUrl("http://" + WebUrl);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
view.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
#Override
public void onPageFinished(WebView view, String url) {
view.refreshDrawableState();
Log.v("FINISH","FINISH");
}
});
Do anybody have an idea why I have this kind of problem.
What version of Android are you testing on? Pre-4.1 versions of Android seem to have this sort problem with WebViews sometimes.
Add this to the manifest for that Activity to fix the problem:
android:hardwareAccelerated="false"
Make WebView invisible in your layout:
<WebView
...
android:visibility="invisible"
.../>
Now, show it back when onPageFinished occurs for the fist time:
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (webView.getVisibility() != View.VISIBLE) {
webView.setVisibility(View.VISIBLE);
}
}
});
I'am not sure, I don't have the whole code, but I think is related to the webViewClient implemented in this function:
public boolean shouldOverrideUrlLoading(WebView view, String url){
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
view.loadUrl(url);
view.setVisibility(View.VISIBLE);
return false; // then it is not handled by default action
}
here is the officiel definition:
public boolean shouldOverrideUrlLoading (WebView view, String url)
Try to test with your code without implementing shouldOverrideUrlLoading, or make it return true.
When loading a page in a webview, I can't reference images on another server - if the page the webview loads is example.com, then
img src="http://anotherexample.com/image.jpg" will not load.
Is there a work-around for this?
its amazing, but for me problem solved by adding shouldOverrideUrlLoading:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
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.
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
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.
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.