"Android" with WebView Loading progress bar (demo download included)

Source: Internet
Author: User

/** * * 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.