Friday 29 July 2016

Display progress bar on Xamarin web view url load or change

Today we are going  to work with Xamarin Webview for Android and show a progress bar on page change. You can implement web view by reading document official website here and can implement progress dialog using following code.
Like this post if it is helpful for you.

Step 1:
Create new project using Visual Studio 2015 File> New > Project
Android > Blank App (Android)
Name Of project : WebViewProgress

Step 2:
Open Resources\Layout\Main.axml
and paste the following code there
<?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"
/>
Save the current file after insert the above code.

Step 3:
Go to the MainActivity.cs and insert the following namespace
using Android.Webkit;
Step 4:
Create instance of Webview inside the class
WebView web_view;
Step 5:
Replace the OnCreate method with the following code
protected override void OnCreate (Bundle bundle)
{
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        web_view = FindViewById<WebView> (Resource.Id.webview);
        web_view.SetWebViewClient(new HelloWebViewClient(this));
        web_view.Settings.JavaScriptEnabled = true;
        web_view.LoadUrl ("http://www.gleamfuture.com");
}
Step 6:
go to the properties and check the INTERNET checkbox see here Step 7: Handle back button\

Step 7:
Activate and handle progress bar

 public class HelloWebViewClient : WebViewClient  
     {  
       public Activity mActivity;  
       public HelloWebViewClient(Activity mActivity)  
       {  
         this.mActivity = mActivity;  
       }  
       ProgressDialog progress;  
       public override bool ShouldOverrideUrlLoading(WebView view, string url)  
       {  
         view.LoadUrl(url);  
         progress = ProgressDialog.Show(mActivity, "Loading...", "Please Wait (about 4 seconds)", true);  
         if (progress != null)  
         {  
           if (progress.IsShowing == false)  
             Toast.MakeText(mActivity, "Loading...", ToastLength.Long).Show();  
         }  
         else  
         {  
           Toast.MakeText(mActivity, "Loading...", ToastLength.Long).Show();  
         }  
         return true;  
       }  
       public override void OnPageStarted(WebView view, string url, Android.Graphics.Bitmap favicon)  
       {  
         Toast.MakeText(mActivity, "Processing...", ToastLength.Long).Show();  
         base.OnPageStarted(view, url, favicon);  
       }  
       public override void OnPageFinished(WebView view, string url)  
       {  
         if (progress != null)  
           if (progress.IsShowing)  
             progress.Dismiss();  
         base.OnPageFinished(view, url);  
       }  
     }  
public override bool OnKeyDown (Android.Views.Keycode keyCode, Android.Views.KeyEvent e)
{
        if (keyCode == Keycode.Back && web_view.CanGoBack ()) {
                web_view.GoBack ();
                return true;
        }

        return base.OnKeyDown (keyCode, e);
}
Its all done you can try it now.
If you have any question please comment here.
Thanks for reading this blog