Write this article, make a memo, simply show a webview example with a progress bar, the progress bar is located above the WebView.
The sample diagram is as follows:
Main Activity code:
Copy Code code as follows:
Package Com.droidyue.demo.webviewprogressbar;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import android.webkit.WebChromeClient;
Import Android.webkit.WebView;
Import Android.widget.ProgressBar;
Import COM.DROIDYUE.DEMO.WEBVIEWPROGRESSBAR.R;
public class Mainactivity extends activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Final ProgressBar bar = (ProgressBar) Findviewbyid (R.id.myprogressbar);
Final WebView WebView = (webview) Findviewbyid (R.id.mywebview);
Webview.setwebchromeclient (New Webchromeclient () {
@Override
public void onprogresschanged (webview view, int newprogress) {
if (newprogress = = 100) {
Bar.setvisibility (view.invisible);
} else {
if (view.invisible = = Bar.getvisibility ()) {
Bar.setvisibility (view.visible);
}
Bar.setprogress (newprogress);
}
Super.onprogresschanged (view, newprogress);
}
});
Findviewbyid (R.id.mybutton). Setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View arg0) {
Webview.reload ();
}
});
Final String url = "Http://jb51.net";
Webview.loadurl (URL);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
}
Layout file Code
Copy Code code as follows:
<relativelayout xmlns: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"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">
<button
Android:id= "@+id/mybutton"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Reload"
/>
<progressbar
Style= "Android:attr/progressbarstylehorizontal"?
Android:id= "@+id/myprogressbar"
android:layout_below= "@id/mybutton"
Android:layout_width= "Match_parent"
android:layout_height= "5px"
/>
<webview
Android:id= "@+id/mywebview"
android:layout_below= "@id/myprogressbar"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
/>
</RelativeLayout>
Do not forget to join the use of network permissions in Mainfest yo.
Copy Code code as follows:
<uses-permission android:name= "Android.permission.INTERNET"/>
The implementation is simple, no technical content. Just a memo.
For information on how to customize the progress bar please refer to: http://www.jb51.net/article/59978.htm