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
Related
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;
}
});
The basic idea is to alter every url being loaded in a webview (for example adding/removing get parameters).
I have a custom WebViewClient, in which I have the following method :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String modifiedUrl = Util.someMethod(url);
super.shouldOverrideUrlLoading(view, modifiedUrl);
}
Will it work or should I put this logic in another method, for example onPagestarted ?
You rather should do something like :
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(conditionForModifyingUrl){
String modifiedUrl = Util.someMethod(url);
view.loadUrl(modifiedUrl);
return true;
}
return false;
}
Calling super.shouldOverrideUrlLoading(view, modifiedUrl) won't work because, as it is its name, this method only checks if the url should be overridden, and does not load the url at all.
I need to implement component that shows list of pictures related to given search query. Pictures are loading from google picture search.
What can I use instead of google picture search?
Parse json from
https://developers.google.com/image-search/?hl=ru (deprecated and limited)
https://developers.google.com/custom-search/docs/overview (free but with limitations)
Run search query in hidden WebView and get list
of images after page loading.
enter code here
query = "https://www.google.com.ua/search?safe=on&site=imghp&tbm=isch&q=milk+buy";
web = (WebView) v.findViewById(R.id.web);
web.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
}
// you tell the webclient you want to catch when a url is about to
// load
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
// here you get all links to pictures
#Override
public void onLoadResource(WebView view, String url) {
Log.i("web link", String.format(url));
if (url.compareTo(query) != 0) {
images.add(url);
}
}
});
web.loadUrl(query);
Check out this example on Github:
https://github.com/koush/ion/blob/master/ion-sample/src/com/koushikdutta/ion/sample/GoogleImageSearch.java
I have a WebView in which I load a page with a custom link (like app://action). I registered the url schemes in the manifest file and when I click on the link, the onResume() method of my Activity is called with the correct data and it works OK.
My problem is that the WebView still try to load the link and my WebView ends up to show a "Web page unavailable" message. I don't want that.
How can I prevent the WebView to load the url?
Here's my code :
WebView banner = ...
banner.setWebViewClient(new WebViewClient() {
#Override
public void onLoadResource(WebView view, String url) {
if (url.startsWith("app://")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url), getContext(), Main.class);
//startActivity(i);
}
}
}
banner.loadUrl("url_to_the_banner");
Use WebViewClient.shouldOverrideUrlLoading instead.
public boolean shouldOverrideUrlLoading(WebView view, String url){
// handle by yourself
return true;
}
WebViewClient Reference
Updates: Method shouldOverrideUrlLoading(WebView, String) is deprecated in API level 24. Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.
But it must return false otherwise, so:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.startsWith(myString){
// handle by yourself
return true;
}
// ...
return false;
}
I'm new to Android app development.
I'm managing to show a WebView and load a given URL. When I click on a link in the WebView, I get a blank white screen.
When I use the Chrome browser on the device (Galaxy TAB), it's working. Actually i'm trying to imitate Chrome in my WebView.
Does anyone know what's the problem?
This is the WebViewClient I use in my WebView:
siteView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
siteView.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (dialog == null || !dialog.isShowing()) {
if(isFirstTime) {
dialog = ProgressDialog.show(MyActivity.this, "", getString(R.string.loadingMessage), true, false);
MyActivity.isFirstTime = false;
}
}
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
try siteView.invalidate()
before loading anything to webview
The problem may lie in your shouldOverrideUrlLoading function. Your are receiving "view" as a parameter and you using "siteView" to load url. Your function should look like:
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
view.loadUrl(urlNewString); // you are using siteView here instead of view
return true;
}
Hope this works for you.
Make sure the url starts with http://. Without http it will just show white screen. Because mostly you will copy url and it will start with www.something.com/asdf. That will not work. change it to http://www.something.com/asdf.