Android Webview Functions - android

In iPhone we have functions like webViewDidStartLoad and webViewDidFinishLoad to check the Start of loading and finish of particular url.
Do we have anythng like this on Android ?

You could try something like this :
webView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Do something here
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Do something here
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// Do something here
}
});

Yes you can use WebViewClient :
private class CustomWebClient extends WebViewClient{
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
}
public void onPageFinished(WebView view, String url)
{
}
}
Usage:
webView.setWebViewClient(new CustomWebClient());
Every method name is self explanatory and you can also check this : http://developer.android.com/reference/android/webkit/WebViewClient.html

Related

Why is android webview onPageStarted being called but onPageFinished is not?

webview.setVisibility(View.VISIBLE);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String URL, Bitmap favicon)
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description,failingUrl);
}
#Override
public void onPageFinished(WebView webview, String url) {
super.onPageFinished(webview, url);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
});webview.loadUrl(“my_url”);

Setting Android WebView platform parameters or flags to Mobile

I am running a WebView in an Android app and need to force either the platform parameters or flags or whatever to be "Mobile" instead of the default "Web". In other words I need the requests (calls) made by the WebView to indicate the platform is for "mobile" or "in-app" and not a "Web" request.
I've found no documentation on how to do this or anything like it. The Android Developer documentation does not offer enough under-the-hood info on how to do the above.
You can set custom webview client and trace all the urls.
WebViewClient myclient = new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Trace the Url here
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
// Trace the Url here
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// Trace the Url here
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Trace the Url here
}
};
mWebView.setWebViewClient(myclient);
Vote Up if this helps.

How can I trigger a function when a webpage is successfully loaded in webview?

I want to trigger a function after webview's finished loading a webpage and received no error. According to my experiment, the onPageFinished() will be triggered several times and in most of the cases, it will be executed before onReceivedError().
So how can I know that the webpage is loaded and no error is received?
You can set the WebViewClient in the WebView and Override its Callback methods which will let you know when the webpage has finished loading the page. For example
webView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
if(progess!=null){
progess.dismiss();
}
progess = ProgressDialog.show(LoadUrl.this, "", "Loading please wait...");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (Uri.parse(url).getHost().equals("callback")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view, url);
// Add YOUR CODE HERE
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(LoadUrl.this, "Sorry! " + description, Toast.LENGTH_SHORT).show();
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();
}
}
Here onPageFinished(WebView view, String url) will be called when webView has finished loading the current Page.
Hope it helps :)

Android Phonegap - TIMEOUT ERROR when trying to set a WebViewClient

I'm working with Android and Phonegap, and at the moment I'm having trouble with one simple thing. I need to setup a webViewClient to the PhoneGap webView in order to capture the URL of a page finished and to work with that.
This is the code:
public class PhoneGapTest extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setBooleanProperty("loadInWebView", true);
super.clearCache();
super.keepRunning = false;
super.loadUrl("file:///android_asset/www/index.html");
super.appView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap bitmap) {
Log.i("TEST", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("TEST", "onPageFinished: " + url);
}
});
}
That code doesn't seems to work, the page never loads and I get a TIMEOUT ERROR, but if I remove the "setWebViewClient" part the page loads perfectly.
I saw that there is a class CordovaWebViewClient, do I have to use that instead of WebViewClient? I found this way on the web:
this.appView.setWebViewClient(new CordovaWebViewClient(this){
#Override
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
Log.i("BugTest", "shouldOverrideUrlLoading: " + url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap bitmap) {
Log.i("TEST", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("TEST", "onPageFinished: " + url);
}
#Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){
}
});
But that code isn't working either, I still got a TIMEOUT ERROR.
I also saw that there is already a webVieClient member, but I don't if I have to use it and how.
I'm working with Phonegap version 1.9.0
Thanks for reading
Answer to Simon:
It worked this way, thanks!
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
super.appView.clearCache(true);
super.appView.clearHistory();
this.appView.setWebViewClient(new CustomCordovaWebViewClient(this));
super.loadUrl("file:///android_asset/www/index.html");
}
public class CustomCordovaWebViewClient extends CordovaWebViewClient {
public CustomCordovaWebViewClient(DroidGap ctx) {
super(ctx);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap bitmap) {
super.onPageStarted(view, url, bitmap);
Log.i("TEST", "onPageStarted: " + url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Log.i("TEST", "onPageFinished: " + url);
}
#Override
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){
super.doUpdateVisitedHistory(view, url, isReload);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
}
I think I've figured this out on latest Cordova versions (I'm using 2.2). It fails at onPageStarted() because it's expecting an appView, which is null. Setting the appView seems to fix it eg
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
CordovaWebViewClient webViewClient = new CustomAcceptingWebViewClient(this);
webViewClient.setWebView(this.appView);
this.appView.setWebViewClient(webViewClient);
super.loadUrl("file:///android_asset/www/index.html");
}
Note that the super.init() is also needed
To accomplish what you want to do I would extend the CordovaWebViewClient class and override the methods you want but don't forget to call the super methods or PhoneGap won't work without the CordovaWebViewClient as it is an important class.
You forgot to call super ;)
// Assign webclient.
this.appView.setWebViewClient(new CordovaWebViewClient(me, this.appView) {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
});

Android - tracking browser activity

I'm looking to develop an application to track how long a site takes to load on the default android browser. Does the browser have any method of tracking when the site is fully loaded?
on page started and on page finished are the methods to
get the the start time and end time of loading of the webview
WebView search_webview;
search_webview = (WebView) findViewById(R.id.show_webview);
search_webview.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
_dialog.dismiss();
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
try{
_dialog.dismiss();
Toast.makeText(ShowWebView.this, "NO Internet Connection Available", Toast.LENGTH_SHORT);
}catch (Exception e) {
// TODO: handle exception
}
}
});

Categories

Resources