Android WebView Behavior for open DeepLinks - android

I've an App, in which there is WebView. When I try to open any Universal link/Deep Link available on the web page loaded in the WebView it should open the targeted app but it open inside the WebView.
and yes I've implemented the shouldOverrideUrlLoading like below from WebViewClient().
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest):
Boolean {
view.loadUrl(request.uri.toString())
return true
}
The behavior I wanting is if user clicks on any normal URL it should open inside WebView and if there is any Universal Link or any Deep Link, it should open the targeted app like all browser apps are doing.
Any Help will be appreciated.
Thanks

I have found a solution on Android Developer page at this link
and now the shouldOverrideUrlLoading block look like below
override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest):Boolean {
val url = request.url.toString()
try {
val intent = Intent(ACTION_VIEW, Uri.parse(url)).apply {
// The URL should either launch directly in a non-browser app (if it's
// the default), or in the disambiguation dialog.
addCategory(CATEGORY_BROWSABLE)
flags = FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_REQUIRE_NON_BROWSER
}
startActivity(intent)
} catch (e: ActivityNotFoundException) {
// Only browser apps are available, or a browser is the default.
// So you can open the URL directly in your app, for example in a
view.loadUrl(url)
}
return true
}
Hope this will helpful for other...

Related

Is there any possible way to handle image opening in web view

I faced a problem, and hope somebody would be kind and give me a hand with it.
The problem is: I've got a webView in my application to open links on some news within my app (and I don't want to open them with browser or anything like that). And this news sometimes contains images.
When user tap on those images, they opens in the same size area as main news. And it looks quite bad.
[in picture bottom you can see top of the opened image][1]
[1]: https://i.stack.imgur.com/6M4fg.jpg
I already have a fragment in my structure to open images by url, but I don't know how to handle this "on-image" click in webView and grab image url from it to go to my ImageView fragment.
I read a bit about using javascript to take id from page html code, but there are no unique id's in html for those pictures.
You can provide your own WebViewClient and override shouldOverrideUrlLoading and then intercept click on a image.
Some example how it can looks like:
webView.webViewClient = object: WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
return if (isImage(request)) {
openImageViewFragment(request.url)
false // indicates that webview shouldn't do anything with the request
} else {
super.shouldOverrideUrlLoading(view, request)
}
}
}

Why is my webview on Android loading slowly?

when I try the link in the browser, it opens very quickly
while on my Android webview, it runs slowly
what should i do so my webview can run faster?
webview.settings.javaScriptEnabled = true
val title = response.body()?.attachment
val filename = "http://lalala.com" + title
webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename)
webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH)
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE)
if (Build.VERSION.SDK_INT >= 19) {
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}
else {
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
webview.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
// do your stuff here
progressbar.visibility = View.GONE
webview.loadUrl("javascript:(function() { " +
"document.querySelector('[role=\"toolbar\"]').remove();})()")
}
}
Unofortunately, there won't be much you can do to fix this. However:
There's a couple of things you can try though, and a few things to check. Specifically:
You're setting the visibility to View.GONE (making your webview invisible) while the page is loading, and then making it visible again when the page has loaded. This is probably the problem.
Try without this, and you will probably find that it will be quicker. In my experience, onPageFinished(..) only fires some time after the page is loaded.
Does the page really require JavaScript ? If not, don't enable it.
If it's feaseable in your case, you can use a HTML parser like Jsoup to extract only the desired data from the page, and show that to the user. This will be a lot faster.
If the page uses Ajax to load data dynamically, you can also load the data directly from the endpoints it uses. Open the page in a desktop browser, and open the network tab of developer tools to find out how the page works and loads data.
You can block requests from the WebView with shouldInterceptRequest(..). This may help if the page has things like eg. Facebook share buttons or extra images which you don't need. Blocking these will speed up load times.
If you show us the URL you're using, maybe I can investigate more and tell you axactly how you could speed it up in your case. Let me know if it helps.

How to set webviewClient for webView dynamically?

For instance, there is a WebView component in my fragment. I use it to load all webpages by the different urls, sort of stupid but efficient. As you know, we specify our webviewclient (like WvjbWebViewClient or jsBridge) to handle all requests. Now that we want to load third-platform websites with our WebView sometimes, however, we don't want to supply our business function for them, not for anything else, but for our safety of communication.
Our company domain is xxx.com. What I want to do is: when the webview loads those webpages on this domain, use our customer WebViewClient, otherwise use a simple WebViewClient(new a instance). How to resolve it? (Should we consider url redirection?)
WebViewClient allows you to upload any specified URL, selected in the WebView, in WebView itself, and do not run the browser. For this functionality meets shouldOverrideUrlLoading(WebView, String) method. If it returns true - we do not need to launch a third-party browser, and upload their own content here.
Here is an example where we choose if we can open the content in our app, or we need to open browser:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("xxx.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
This can help if think.

How do I open another app in my WebView app?

I have an Android app with that displays a mobile website (WebView), in the mobile website there are links redirecting to a PDF, Excel and video files.
When try to open it in my regular browser my phone asks to open it with another app or it start a download, so I can open it afterwards.
But in my WebView app it either doesn't work, no response or it displays a "Page unavailable" error.
Is it even possible?
To handle links in WebView, you can use the shouldOverrideUrlLoading method of WebViewClient class. Consider the following example;
WebView webView = (WebView) findViewById(R.id.infoView);
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Assuming you are giving link to some PDF file.
if (url.contains(".pdf")) {
// Now do what you want to with the url here
}
return true;
}
}
This way, you can intercept any link tapped in WebView and then do whatever you want.

How to get Webview iframe link to launch the browser?

I'm using a WebView to display a page in which the html includes an iframe where src="xxxxx.php".
This iframe loads as an ad image with an underlying link. If I click on that image (link), it tries to load the new page within original iframe (which doesn't show much in that little space). What I want to happen is clicking on the link to open the referred page in a new browser window, leaving my app as is.
If I use the Android browser to display the original page and click on this iframe, it loads the link as a new page. How do I get the same behavior with a WebView? Using a WebViewClient with shouldOverrideUrlLoading() doesn't seem to be called by the iframe link.
I had a similar issue with google ads in a WebView source, since they load in an iframe as well. This is how I resolved it:
Try this in your WebViewClient, typically under your shouldOverrideUrlLoading()
#Override
public void onLoadResource (WebView view, String url) {
if (url.contains("googleads")) {
if(view.getHitTestResult().getType() > 0){
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
view.stopLoading();
Log.i("RESLOAD", Uri.parse(url).toString());
}
}
}
I can propose one fix to previous code:
#Override
public void onLoadResource (WebView view, String url) {
if (url.contains("googleads")) {
if(view.getHitTestResult() != null &&
(view.getHitTestResult().getType() == HitTestResult.SRC_ANCHOR_TYPE ||
view.getHitTestResult().getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)){
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
view.stopLoading();
}
}
}
To detect links clicks inside of the iframe. The links should have additional parameters. I've found that:
shouldOverrideUrlLoading of the WebViewClient will be executed for a case when iframe link has target parameter target ="_parent" or target="_top",
onCreateWindow of the WebViewClient, in case, if the iframe link contains target="_blank" parameter
Seems the iframe links without target parameter not possible to track exactly via WebViewClient

Categories

Resources