The method of implementing WebView top with progress bar in Android _android

Source: Internet
Author: User

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

Related Article

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.