/** * * This webviewwithprogress inherits from Relativielayout, * if you want to set the properties of WebView, first call Getwebview () to get an instance of * WebView * @author administr Ator * */public class Webviewwithprogress extends Relativelayout{private Context context;private WebView mwebview = null; The horizontal progress bar private ProgressBar ProgressBar = null;//contains the layout of the circular progress bar private relativelayout progressbar_circle = null; The progress bar style, Circle is represented as a circle, horizontal is represented as horizontal private int mprogressstyle = ProgressStyle.Horizontal.ordinal (); Default horizontal progress bar height public static int default_bar_height = 8;//horizontal progress bar high private int mbarheight = Default_bar_height; public enum progressstyle{horizontal,circle;}; Public webviewwithprogress (Context context) {super (context);//TODO auto-generated Constructor Stubthis.context = Context;init ();} Public webviewwithprogress (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor Stubthis.context = context; TypedArray attributes = Context.obtainstyledattributes (attrs,r.styleable.webviewwithprogress); MproGressstyle = Attributes.getint (r.styleable.webviewwithprogress_progressstyle,progressstyle.horizontal.ordinal ()) ; Mbarheight = Attributes.getdimensionpixelsize (R.styleable.webviewwithprogress_barheight, DEFAULT_BAR_HEIGHT); Attributes.recycle (); init ();} private void Init () {Mwebview = new WebView (context); This.addview (Mwebview, Layoutparams.fill_parent, Layoutparams.fill_parent); if (Mprogressstyle = = ProgressStyle.Horizontal.ordinal ()) {ProgressBar = (ProgressBar) Layoutinflater.from (context). Inflate (r.layout.progress_horizontal, null);p Rogressbar.setmax (100); Progressbar.setprogress (0); WebViewWithProgress.this.addView (ProgressBar, layoutparams.fill_parent, mbarheight);} Else{progressbar_circle = (relativelayout) layoutinflater.from (context). Inflate (r.layout.progress_circle, NULL); WebViewWithProgress.this.addView (Progressbar_circle, layoutparams.fill_parent, layoutparams.fill_parent);} Mwebview.setwebviewclient (New Webviewclient () {@Overridepublic Boolean ShouldoverrideurlloadinG (WebView view, String URL) {//TODO auto-generated method Stubview.loadurl (URL); return true;}}); Mwebview.setwebchromeclient (New Webchromeclient () {@Overridepublic void onprogresschanged (WebView view, int newprogress) {//TODO auto-generated method stubsuper.onprogresschanged (view, newprogress); if (newprogress = =) {if ( Progressbar!=null) {progressbar.setvisibility (view.gone);} if (progressbar_circle!=null) {progressbar_circle.setvisibility (View.gone);}} Else{if (Mprogressstyle = = ProgressStyle.Horizontal.ordinal ()) {progressbar.setvisibility (view.visible); Progressbar.setprogress (newprogress);} Else{progressbar_circle.setvisibility (view.visible);}}}); Public WebView Getwebview () {return mwebview;}}
used in XML:
<com.example.webviewwithprogress android:id= "@+id/detail_webview" android:layout_width= "Match_ Parent " android:layout_height=" match_parent " android:background=" @color/page_bg " webviewwithprogress:progressstyle= "Horizontal" />
Set the WebView property in your code:
Mwebviewwithprogress = (webviewwithprogress) Findviewbyid (r.id.detail_webview); Mwebview = Mwebviewwithprogress.getwebview (); Mwebview.getsettings (). setjavascriptenabled (false); Mwebview.getsettings (). Setlayoutalgorithm (Layoutalgorithm.single_column); Mwebview.setscrollbarstyle ( Webview.scrollbars_outside_overlay);
Effect:
Demo Download: WebView with loading progress bar
"Android" with WebView Loading progress bar (demo download included)