java - How to add Progress bar while loading webpage on Android studio Webview -


hi newbie here , android development , want add progress bar while loading page on webview , after loading progress need hide automatically, dont know how , me guys

activity_main.xml:

  <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="0dp"     android:paddingleft="0dp"     android:paddingright="0dp"     android:paddingtop="0dp"     tools:context="com.example.tamil.stagingsite.mainactivity">      <progressbar         android:id="@+id/progressbar"         style="@android:style/widget.material.light.progressbar.large"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerhorizontal="true"         android:layout_centervertical="true" />      <webview         android:id="@+id/activity_main_webview"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_alignparentstart="true"         android:layout_alignparenttop="true"         android:animationcache="true"         android:background="@android:color/white">      </webview> </relativelayout> 

mainactivity.java

package com.example.tamil.stagingsite;  import android.app.activity; import android.graphics.bitmap; import android.net.uri; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.view.window; import android.webkit.websettings; import android.webkit.webview; import android.webkit.webviewclient; import android.widget.progressbar;  import com.google.android.gms.appindexing.action; import com.google.android.gms.appindexing.appindex; import com.google.android.gms.appindexing.thing; import com.google.android.gms.common.api.googleapiclient;  public class mainactivity extends activity {       private webview mwebview;     /**      * attention: auto-generated implement app indexing api.      * see https://g.co/appindexing/androidstudio more information.      */     private googleapiclient client;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         requestwindowfeature(window.feature_no_title);         setcontentview(r.layout.activity_main);          mwebview = (webview) findviewbyid(r.id.activity_main_webview);          websettings websettings = mwebview.getsettings();         websettings.setjavascriptenabled(true);         mwebview.loadurl("http://census-staging.herokuapp.com/");         mwebview.setwebviewclient(new webviewclient());         mwebview.setwebviewclient(new myappwebviewclient());          // attention: auto-generated implement app indexing api.         // see https://g.co/appindexing/androidstudio more information.         client = new googleapiclient.builder(this).addapi(appindex.api).build();     }       @override     public void onbackpressed() {         if (mwebview.cangoback()) {             mwebview.goback();         } else {             super.onbackpressed();         }     }       /**      * attention: auto-generated implement app indexing api.      * see https://g.co/appindexing/androidstudio more information.      */     public action getindexapiaction() {         thing object = new thing.builder()                 .setname("main page") // todo: define title content shown.                 // todo: make sure auto-generated url correct.                 .seturl(uri.parse("http:staging.herokuapp.com/"))                 .build();         return new action.builder(action.type_view)                 .setobject(object)                 .setactionstatus(action.status_type_completed)                 .build();     }      @override     public void onstart() {         super.onstart();          // attention: auto-generated implement app indexing api.         // see https://g.co/appindexing/androidstudio more information.         client.connect();         appindex.appindexapi.start(client, getindexapiaction());     }      @override     public void onstop() {         super.onstop();          // attention: auto-generated implement app indexing api.         // see https://g.co/appindexing/androidstudio more information.         appindex.appindexapi.end(client, getindexapiaction());         client.disconnect();     } } 

to add progress dialog while loading page on webview,add following code , no need add progressbar in xml.

//intialize progress dialog:   webview webview;   progressdialog progressdialog;  //write following code in oncreate:      webview=(webview)findviewbyid(r.id.webview);     progressdialog=new progressdialog(this);     progressdialog.setmessage("loading");     progressdialog.setprogressstyle(progressdialog.style_spinner);     progressdialog.show(); 

after loading webpage, have dismiss progressdialog.for in onpagefinish dismiss progressdialog.

webview.setwebviewclient(new webviewclient()     {         @override         public boolean shouldoverrideurlloading(webview view, string url) {             view.loadurl("your url");             return true;         }          @override         public void onpagefinished(webview view, string url) {             super.onpagefinished(view, url);             if (progressdialog.isshowing()) {                 progressdialog.dismiss();             }         }     }); 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -