How To Link To An Android App From Android Webview - android

Currently I have an Android app that is basically a web view loading a web page. On the web page I've tryed to link to the market like this...
https://market.android.com/details?id=com.google.earth
http://market.android.com/details?id=com.google.earth
market://details?id=com.google.earth
The first result just opens up a white screen (It may be loading, but it has been their for over a minute).
The second result says that the page has been moved and a link. If you click th link it does what the first one did.
The third result says that the page may be temporarily down. (It's treating the link like its online rather than in the phone itself)
Here is how the link looks...
echo "<a href='https://market.android.com/details?id=com.google.earth' data-role='button'>Upgrade Now</a>";
Remember the web page I'm loading is using JQuery Mobile and I'm echoing the link with php.
How can I open a link to the Android Market in a webview on a web page?

In my case, I was getting the same blank/moved temporarily trouble described. I wanted to use the shouldOverrideUrlLoading so that the native browser wasn't used in an oauth2 flow from my page to google and back to my page. My android app was talking to localhost/tomcat with a self-signed cert. Turned out I needed to call proceed because of cert mismatch:
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("DevAgentSocialMain", "URL: " + url);
view.loadUrl(url);
return true;
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i("DevAgentSocialMain", "onReceivedError: " + errorCode + " " + description + " url: " + failingUrl);
super.onReceivedError(view, errorCode, description, failingUrl);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.i("DevAgentSocialMain", "onReceivedSslError: " + error.getPrimaryError());
//super.onReceivedSslError(view, handler, error);
handler.proceed();
}
});

You can use a callback when a user clicks a link inside the webview.
See
Handling Page Navigationsection on the android developer platform.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(MyWebViewClient);
Then your callback
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}

Related

Android. WebView displaying white page

Why the web view can show a white page, although the page opens in the browser. At first there was an error:
net::ERR_CACHE_MISS
but after I added this code:
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
there are no errors, but the page does not load, just a white page is displayed. The page opens by clicking on the button inside in webview.Clicking is not processed on the mobile application side.
My manifest also has permission:
<uses-permission android:name="android.permission.INTERNET" />
Please help me. I don’t understand why this can happen. The page also opens successfully on iOS.
Here is my code:
private void initView(String url) {
wvDetail.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("webview://closeScreen")) {
activity.finish();
return true;
} else
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(activity, description, Toast.LENGTH_LONG).show();
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
});
wvDetail.getSettings().setJavaScriptEnabled(true);
wvDetail.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wvDetail.loadUrl(url);
}
It seems to be a problem with the settings of your webview. Try and add the following settings:
WebSettings settings = wvDetail.getSettings();
//if your page needs javascript
settings.setJavaScriptEnabled(true);
//to handle your cache
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
settings.setAppCacheEnabled(true);
settings.setAppCachePath(cacheDir.path);
To enable some functionalities of JS you should also use setdomStorageEnabled (see this question):
settings.setDomStorageEnabled(true)
If your page is still not loading and you fully trust the web page that you will load, I would also use this settings to give even more access to the webview:
settings.setBlockNetworkImage(false);
settings.setLoadsImagesAutomatically(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
settings.setSafeBrowsingEnabled(false);
}
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setAllowFileAccess(true);
To load the webview with the settings of the meta tag, use:
setUseWideViewPort(true);
Finally you could check upon your webView size and make it fit on your system window like this:
wvDetail.setFitsSystemWindows(true);
Just like a bonus if you are targetting above API level 11, you could set your layer type like this:
wvDetail.setLayerType(View.LAYER_TYPE_HARDWARE, null);
I hope this explanation of the common settings used in a webview makes sense! Depending on your webpage I think that some of them can be useful to you! let me know if it works! :)

Doesn't catch redirect in shouldOverrideUrlLoading

Edit:
I found out that the web site is a single page website and that's probably the reason I didn't catch the change of the url so now my question is: Can I know the url changed on a single page website?
I have a webView which load a web site.
shouldOverrideUrlLoading called only once, on the first load of the page but not when it redirected inside the web.
This is the code of the webClient I use:
private class WebViewClientServiceGuide extends MyWebViewClient {
private WebView wv;
public WebViewClientServiceGuide(WebView _wv) {
wv = _wv;
Log.d(TAG, "WebViewClientServiceGuide.ctor");
}
public void onLoadResource(WebView view, String url) {
Log.d(TAG, "WebViewClientServiceGuide - onLoadResource: " + url);
super.onLoadResource(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
//Add history to history manager if needed
Log.d(TAG, "WebViewClientServiceGuide - onPageFinished");
super.onPageFinished(view,url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
Log.d(TAG, "WebViewClientServiceGuide - shouldOverrideUrlLoading(WebView view, WebResourceRequest request)");
return super.shouldOverrideUrlLoading(view, request);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "WebViewClientServiceGuide - shouldOverrideUrlLoading");
return false;
}
}
Lets say I visited these pages: page1, page2, page3 but catches only page1 on shouldOverrideUrlLoading. But when I examined the history of the webView (copyBackForwardList) I saw all the list (page1, page2, page3).
Why does it happens? (I tried to use other website and catches there all the pages - I know the web site developer should fix it but I want to know why the history stack is different than the urls I catch on shouldOverrideUrlLoading )
Thank you
Found the solution:
I overrode a method of WebViewClient called doUpdateVisitedHistory which gets a url, even on single page website

Android application is stuck on links in Android Webview

Background
I am making an app which uses Quora links All Links(profiles,answers,topics,except sign in) are Opening in My App only .
Problem
There is a option "Open in App" , I want to Remove that
Option.Clicking on the Cross Button Doesn't work.
User Can't Sign in the Quora , Sign in Link is not opening.
In both cases the app is stuck .I tried WebView Client Solution that is
working fine but for sign in case it is not working.
There are two require that you need:
Hide element of website (here is "Open in App" button)
Disable event of element in website (here is signin click)
and solution you can found at WebViewClient class:
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(!url.equals(currentUrl)){ //currentUrl is url that signin with navigated.
view.loadUrl(url);
}
return true;
}
#Override
public void onPageFinished(WebView view, String url)
{
// hide element by class name
webview.loadUrl("javascript:(function() { " +
"document.getElementsByClassName('your_class_name')[0].style.display='none'; })()");
// hide element by id
webview.loadUrl("javascript:(function() { " +
"document.getElementById('your_id')[0].style.display='none';})()");
}
});

Webview doesn't show a specific website

I'm trying to do a webview based application for this website to show it in a mobile application.
when I put any different site the application work great, but in this specific site never show the second page when I clicked in the button to go to the desk page. However, I put a Log statement in the onPageFinished method and log that the page is loaded completely.
My Code Here
final WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getActivity(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onPageFinished(WebView view, String url) {
Log.d("WEBSITE", "Page Loaded.");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("WEBSITE", url);
myWebView.loadUrl(url);
return false;
}
});
myWebView.loadUrl("https://demo.frappecloud.com/");
I believe the problem is with your shouldOverrideUrlLoading. If you check the documentation you will see that :
This method is not called for requests using the POST "method"
When you are submitting the Form, you are making a POST request, which is basically ignored.
Here is the link: WebViewClient
Also check this reference where it says how you could load a URL with POST data: LOAD A POST REQUEST INTO A WEBVIEW IN ANDROID
Consider checking this thread: Android - how to intercept a form POST

Android - WebViewClient

I am trying to load URL inside a WebViewClient as below:
webview.setWebViewClient(new WebViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url)
{
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
}
});
webview.loadUrl(articleLink);
Problem:
Web URL is loading successfully but in some occurrence i got the following errors, at the same time i would like to display Alert Dialog instead of Webview with Error message.
So can you please let me know ,How do i handle the following kind of errors:
"Web page not available" error
"Directory listing Denied"
I have attached the 2nd one's image for your reference:
Both of those are server side errors.
1. is the page is either physically not there (404) or a
2. the server is serving you a page that states it will not show you the directory (200)
but you should be able to handle them inside the
OnReceivedError()
you can create a dialog box from there.

Categories

Resources