1. Custom Progresswebview
Packagecom.app.android05;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.util.AttributeSet;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient;ImportAndroid.widget.ProgressBar;/** * @authorAdmin * WebView with progress bar*/ Public classProgresswebviewextendsWebView {Privatecontext Context; PrivateProgressBar ProgressBar; PrivateOnwebcallback Onwebcallback;//Callback PublicProgresswebview (Context context) { This(Context,NULL ) ; } PublicProgresswebview (Context context, AttributeSet attrs) { This(context, Attrs, Android.) R.attr.webtextviewstyle); } PublicProgresswebview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); This. Context =context; Init (); Setwebviewclient (Newmywebviewclient ()); Setwebchromeclient (Newwebchromeclient ()); } /*** Set ProgressBar*/ voidinit () {ProgressBar=NewProgressBar (Context,NULL, Android. R.attr.progressbarstylehorizontal); Progressbar.setlayoutparams (NewLayoutparams (layoutparams.match_parent, 20, 0, 0 )); AddView (ProgressBar); } Public classWebchromeclientextendsandroid.webkit.WebChromeClient {@Override Public voidOnprogresschanged (WebView view,intnewprogress) { if(newprogress = = 100) {progressbar.setvisibility (GONE); } Else{progressbar.setvisibility (VISIBLE); Progressbar.setprogress (newprogress); } Super. onprogresschanged (view, newprogress); } @Override Public voidOnreceivedtitle (WebView view, String title) {Super. Onreceivedtitle (view, title); if(Onwebcallback! =NULL){//Get titleOnwebcallback.gettitle (title); } } } /*** Do not rewrite, will jump to the mobile browser *@authorAdmin*/ Public classMywebviewclientextendswebviewclient {@Override Public voidOnreceivederror (WebView view,intErrorCode, string description, String failingurl) { //Handle theGoBack (); } @Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true; } @Override Public voidonpagefinished (WebView view, String URL) {Super. onpagefinished (view, URL); } @Override Public voidonpagestarted (WebView view, String URL, Bitmap favicon) {if(Onwebcallback! =NULL){//get the address of WebViewonwebcallback.geturl (URL); } }} @Overrideprotected voidOnscrollchanged (intLintTintOLDL,intOldt) {Layoutparams LP=(Layoutparams) progressbar.getlayoutparams (); Lp.x=l; LP.Y=T; PROGRESSBAR.SETLAYOUTPARAMS (LP); Super. onscrollchanged (L, T, OLDL, Oldt); } /*** Set the webview of the device *@paramOnwebcallback*/ voidSetonwebcallback (Onwebcallback onwebcallback) { This. Onwebcallback =Onwebcallback; } }Interfaceonwebcallback{/*** Get title *@paramtitle*/ voidGetTitle (String title); /*** Get WebView's address *@paramURL*/ voidgetUrl (String URL);}
2. XML reference
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.app.android05.mainactivity$placeholderfragment" > <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "title" /> <TextViewAndroid:id= "@+id/url"Android:layout_below= "@id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "url" /> <Com.app.android05.ProgressWebViewAndroid:id= "@+id/web"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:layout_below= "@id/url" /></Relativelayout>
3. Mainactivity Call
Packagecom.app.android05;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateTextView Title_tv; PrivateTextView Url_tv; PrivateProgresswebview WebView; PrivateString url = "http://dict.youdao.com/" ; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TITLE_TV=(TextView) Findviewbyid (r.id.tv); URL_TV=(TextView) Findviewbyid (R.id.url); WebView=(Progresswebview) Findviewbyid (R.id.web); //set WebView back function to get URLWebview.setonwebcallback (NewOnwebcallback () {//Get title@Override Public voidGetTitle (String title) {Title_tv.settext (title); } //gets the URL address of the current web@Override Public voidgetUrl (String url) {url_tv.settext (URL); } }); Webview.loadurl (URL); }}
Customize WebView with progress bar, add get web title and URL back