Skip to content

Instantly share code, notes, and snippets.

@pokk
Last active December 16, 2016 07:28
Show Gist options
  • Save pokk/0a1a9631c1978561a6d4120366f0223b to your computer and use it in GitHub Desktop.
Save pokk/0a1a9631c1978561a6d4120366f0223b to your computer and use it in GitHub Desktop.
change URL request or set an action in WebViewClient

Introduction

Changing the default action before loading a url link.

public class MyWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// Get the url final filename extension of url file.
String filenameArray[] = URLUtil.guessFileName(url, null, null).split("\\.");
String extension = filenameArray[filenameArray.length - 1];
if (extension.equals("jpg"))
{
// ♦♦ Open a image view to fit your device screen size.
String html = "<html>" +
"<head>" + "<title>" + "Image" + "</title>" +"</head>" +
"<body>" + "<center>" +
"<img src=\"" + url + "\" style=\"display: inline;height: auto;max-width: 100%;\" />" +
"</center>" + "</body>" +
"</html>";
view.loadData(html, "text/html", null);
// This is important part. Before 'return true', you could do any action what you need.
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment