detecting when phonegap webview finishes loading android - android

Does PhoneGap (more specifically DroidGap for Android) have way to tell when its webview has been loaded. I'd like to find something like the following for PhoneGap:
Normal WebView Client Creation:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
Creating my PhoneGap Webview like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
if (isOnline() == true) {
WebSettings settings = mWebView.getSettings();
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);
settings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://myurl.com");
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.mWebView).setVisibility(View.VISIBLE);
}
});
}
else {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.mWebView).setVisibility(View.VISIBLE);
Intent myIntent = new Intent(getContext(), LoadScreen.class);
startActivity(myIntent);
finish();
}
}
My Main_Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView android:id="#+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="#drawable/splash"
/>
<WebView android:id="#+id/mWebView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
</RelativeLayout>

DroidGap does not have a method, since it's an activity, and not a view. Instead, the CordovaWebViewClient uses its onPageFinished method to send a message to all the plugins when the page has finished loading. This is what plugins can use to determine whether the page has finished. Conversely, you could check for the deviceready event in Javascript, which should be fired after the page has been loaded.
If you need to know when the WebView has loaded in Java for anything other than plugins, I highly recommend switching to using CordovaWebView and implementing the lifecycle events yourself, since this will give you more control over the augmented view. This also means that you have to make sure that plugins which use activities are handled correctly, otherwise they will break.

Related

Open up chrome custom tab from within Android webview: CustomURL handler & insecure content warning

I'm trying to open up a Chrome custom tab from within a webview inside of an Android app. In order to do this, I figured I could register a custom URL handler(customtab://www.myurl.com/). The error I'm running into is that chromium(the webview) is blocking the custom URL handler due to an insecure content error (This request has been blocked; the content must be served over HTTPS.). The URL is https. Even adding webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); on the webView didn't work.
Any ideas on how I can register this custom handler as "secure"?
You can use web intent to open url in chrome
Intent webIntent = new Intent(Intent.ACTION_VIEW);
webIntent.setData(Uri.parse(url));
getActivity().startActivity(webIntent);
You need to override this method on the Webview to skip ssl errors
public synchronized void onReceivedSslError(WebView view, final SslErrorHandler handler,SslError error) {
handler.proceed();
}
I am open my custom URL in web view in android with follow below process.
Design your webview as full screen like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="20dp"
android:gravity="center"
android:background="#ffffff">
<WebView
android:id="#+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"/>
</RelativeLayout>
And in you .java file add below code in onCreate method
webView = (WebView) findViewById(R.id.WebView);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
getWindow().setLayout(windowwidth, windowheight);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(false);
settings.setDomStorageEnabled(true);
try {
// here i call progressdialog which is load still the page is not load //properly
Constants.ShowProgressDialog(MyActivity.this, "", "Please wait...");
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("", "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i("", "Finished loading URL: " + url);
// dismiss the progressbar after load the web page in webview.
Constants.disMisProgressdialog();
}
#Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
});
webView.loadUrl("<your url>");
} catch (Exception e) {
e.printStackTrace();
}
If still any issue please provide me your url I'll check and provide you the code.
Hope your problme resolve.
Make sure provide permission in Manifest file.
"<"uses-permission android:name="android.permission.INTERNET" />" (remove the quatation mark)
You simply need to register a WebViewClient in your WebView and override the shouldOverrideUrlLoading() method to support your own scheme:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("custometab://")) {
// TODO: start custom tabs for the url.
return true;
} else {
return false;
}
}
(see https://stackoverflow.com/a/5514668/94363)
The problem is: how do you know when clicking on customtab://www.myurl.example.com/ if you should open http://www.myurl.example.com or https://www.myurl.example.com?
You could either introduce customtab:// and customtabs://
or do some manipulation of the URL customtab://http/www.myurl.example.com
But in fact, I don't think we need a custom scheme at all if you want to open all links with custom tabs. Just use:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO: start custom tabs for the url
return true;
}

Intent on Button Click inside webview in android

enter code hereI am new to android development. I am currently working on an app where it uses webview. In that webview I am passing an url and when the url loads that corresponding page has a button inside the webpage that has the functionality to exit the Webpage. My requirement is to get the click function of that button. How is it possible to implement it in android. If anyone knows please help
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
public class WebviewActivity extends Activity {
WebView web;
String rooId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Intent intent = getIntent();
rooId = intent.getStringExtra("RoomId");
Log.e("Roominer", "Roominer" + rooId);
web = (WebView) findViewById(R.id.webView1);
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);
web.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
web.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("", "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i("", "Finished loading URL: " + url);
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.LENGTH_SHORT).show();
}
}
To link your Java code and Javascript code together, you can use something called a JavascriptInterface
This guide will give you a good idea to set this up.
https://developer.android.com/guide/webapps/webview.html
Don't forget to add the #JavascriptInterface annotation above the methods you want to use. This was implemented in SDK 17:
Caution: If you've set your targetSdkVersion to 17 or higher, you must add the #JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.
You can defined a button in your xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and set onClickListener at the button:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Put the action
}
});

Full Screen Webview without URL Address bar

We are trying to wrap a website within a mobile application. This website supports Chrome, IE9 and above and Safari. While trying to create an Android application to wrap the website, below is the layout which is created
<?xml version="1.0" encoding="utf-8"?>
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Here is my code
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
new GcmRegistrationAsyncTask(this).execute();
WebView myWebView = (WebView) findViewById(R.id.webview);
Intent intent = getIntent();
String url = intent.getStringExtra(Constants.URL);
myWebView.setWebChromeClient(new WebChromeClient());
//myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl(url);
}
If I just use WebViewClient, website works properly in full screen mode but as it is not compatible with Android Viewer, website is not seen properly. If i use WebChromeClient, it opens up the website properly in separate browser (Chrome) but shows URL Address bar. I went through lot of posts on the same but couldn't figure out the solution. Here is my end goal
I need to open the website in mobile application and i want to hide the address title bar. How can i achieve this?
I use the following and it works just like you want
WebView webView = (WebView) findViewById(R.id.webview);
ProgressBar progressBar = (ProgrssBar) findViewById(R.id.progressBar);
String url = "https://www.google.com";
progressBar.setIndeterminate(false);
progressBar.setMax(100);
WebSettings.ZoomDensity zoomDensity = WebSettings.ZoomDensity.FAR;
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDefaultTextEncodingName("utf-8");
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setDefaultZoom(zoomDensity);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.requestFocus(View.FOCUS_DOWN);
webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webView.setWebChromeClient(webChromeClient);
webView.setWebViewClient(this.getWebViewClient());
progressBar.setProgress(0);
progressBar.setVisibility(View.VISIBLE);
webView.loadUrl(url);
And the WebChromeClient and WebViewClient implementations as;
WebChromeClient webChromeClient = new WebChromeClient() {
#Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressBar.setProgress(newProgress);
}
};
public WebViewClient getWebViewClient() {
return new WebViewClient() {
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
progressBar.setVisibility(View.GONE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
progressBar.setVisibility(View.VISIBLE);
//Some checks
if (url.contains("load.elsewhere.com")) {
//If you want to handle where load.elsewhere.com is loaded, say in another external browser
progressBar.setIndeterminate(true);
//startActivity to load elsewhere
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
};
}
The ProgressBar is something I add to show some progress
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</FrameLayout>
WebView does not have an address bar by default something else must be forwarding it to your browser. The API Guide for WebViews shows this example to override shouldOverrideUrlLoading:
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;
}
}
This will keep your app in your app and offsite links can be handled by the device's browser.

How to show the web content in our android app without using browser

Here i am developing an android app as well as web page. I need to show the web content in my android activity without using browser.
I am using WebView for this requirement. But the problem is if i click the button it opens in browser.
Here i have to display that page in my android app as new activity.
This is the code i have tried for this issue.
webview.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:ignore="MergeRootFrame">
<WebView
android:id="#+id/activity_tab_host_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
this is my Admissionactivity which need to show the web content
public class AdmissionActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
webView = (WebView) findViewById(R.id.activity_tab_host_webview);
webView.loadUrl("http://www.google.com");
}
}
this is the code for the button click
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_college_listview);
intro = (TextView) findViewById(R.id.intro_button);
intro.setText("Introduction");
intro.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent admAct = new Intent(getApplicationContext(), AdmissionActivity.class);
// Clears History of Activity
admAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(admAct);
}
});
}
Just implement the web client and set it before loadUrl. The simplest way is:
myWebView.setWebViewClient(new WebViewClient());
//prevent web browser launch with loading http url
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
});

Getting net::ERR_UNKNOWN_URL_SCHEME while calling telephone number from HTML page in Android

I am getting "net::ERR_UNKNOWN_URL_SCHEME" while calling a telephone number option from an HTML page in Android. Do I need to add any permission(s) in the manifest to get this working? I haven't added anything in the manifest so far. Here's the HTML Code:
Call us free!
The following should work and not require any permissions in the manifest (basically override shouldOverrideUrlLoading and handle links separately from tel, mailto, etc.):
mWebView = (WebView) findViewById(R.id.web_view);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle things like tel, mailto, etc.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true;
}
});
mWebView.loadUrl(url);
Also, note that in the above snippet I am enabling JavaScript, which you will also most likely want, but if for some reason you don't, just remove those 2 lines.
I had this issue occurring with mailto: and tel: links inside an iframe (in Chrome, not a webview). Clicking the links would show the grey "page not found" page and inspecting the page showed it had a ERR_UNKNOWN_URL_SCHEME error.
Adding target="_blank", as suggested by this discussion of the issue fixed the problem for me.
Try this way,hope this will help you to solve your problem.
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MyActivity.java
public class MyActivity extends Activity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
webView.loadData("Call us free!", "text/html", "utf-8");
}
}
Please add this permission in AndroidManifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"/>

Categories

Resources