The progress bar of WebView in Android

Source: Internet
Author: User

    < Application         Android:icon = "@drawable/hunqin"         android:label= "@string/app_name"        android:theme= "@android: style/ Theme.light ">

Theme Android:theme-------> must not make Theme.Light.NoTitleBar

Otherwise, it doesn't work.

requestwindowfeature (window.feature_progress);        Setcontentview (R.layout.progressbar);        Setprogressbarvisibility (true);

    < Application         Android:icon = "@drawable/hunqin"         android:label= "@string/app_name"        android:theme= "@android: Style/theme.light.notitlebar ">

 Public classProgresstestextendsactivity{FinalActivity context = This; @Override Public voidonCreate (Bundle b) {Super. OnCreate (b); Requestwindowfeature (window.feature_progress);//let the progress bar appear on the title barSetcontentview (R.layout.main); WebView WebView=(WebView) Findviewbyid (R.id.webview); Webview.setwebchromeclient (Newwebchromeclient () { Public voidOnprogresschanged (WebView view,intprogress) {                //activity and WebView determine the progress size of the progress bar depending on the load level//the progress bar disappears automatically when loading to 100%Context.setprogress (Progress * 100);   }    }); Webview.loadurl (URL);}

Http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html

Objective

If you do not use the system's own titlebar (that is, activity is set @android:style/theme.notitlebar), then you need to write the progress bar, which encapsulates a custom control and loading the Web page of the public activity, easy to use.

Statement

Welcome reprint, but please keep the original source of the article:)

Blog Park: http://www.cnblogs.com

Farmer Uncle: Http://over140.cnblogs.com

Body

One

Second, custom controls

/**
* WebView with a progress bar
* @author Peasant Uncle
* @see http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
*
*/
@SuppressWarnings ("deprecation")
public class Progresswebview extends WebView {

Private ProgressBar ProgressBar;

Public Progresswebview (context context, AttributeSet Attrs) {
Super (context, attrs);
ProgressBar = new ProgressBar (context, NULL, Android. R.attr.progressbarstylehorizontal);
Progressbar.setlayoutparams (New Layoutparams (layoutparams.fill_parent, 3, 0, 0));
AddView (ProgressBar);
Setwebviewclient (New Webviewclient () {});
Setwebchromeclient (New Webchromeclient ());
}

public class Webchromeclient extends Android.webkit.WebChromeClient {
@Override
public void onprogresschanged (WebView view, int newprogress) {
if (newprogress = = 100) {
Progressbar.setvisibility (GONE);
} else {
if (progressbar.getvisibility () = = GONE)
Progressbar.setvisibility (VISIBLE);
Progressbar.setprogress (newprogress);
}
Super.onprogresschanged (view, newprogress);
}

}

@Override
protected void onscrollchanged (int l, int t, int oldl, int Oldt) {
Layoutparams LP = (layoutparams) progressbar.getlayoutparams ();
lp.x = l;
LP.Y = t;
PROGRESSBAR.SETLAYOUTPARAMS (LP);
Super.onscrollchanged (L, T, OLDL, Oldt);
}
}

Third, loading the Web page of the public activity

/**
* Load the activity of the webpage
*
* @author Peasant Uncle
* @see http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html
*
*/
public class Webactivity extends Baseactivity {

Private Progresswebview WebView;
Private String URL;
private String name;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.commom_web);

~ ~ ~ Get Parameters
url = getintent (). Getstringextra ("url");
Name = Getintent (). Getstringextra ("name");

~ ~ ~ Bound Control
WebView = (Progresswebview) Findviewbyid (R.id.webview);

~ ~ ~ Setting Data
Titletext.settext (name);
Webview.getsettings (). Setjavascriptenabled (True);
Webview.setdownloadlistener (New Downloadlistener () {
@Override
public void Ondownloadstart (string url, String useragent, String contentdisposition, String mimetype, long ContentLength) {
if (URL! = null && url.startswith ("/http"))
StartActivity (New Intent (Intent.action_view, Uri.parse (URL)));
}
});

Webview.loadurl (URL);
}
}

Commom_web.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

<include layout= "@layout/include_title"/>

<com.nmbb.ui.widget.progresswebview
Android:id= "@+id/webview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"/>

</LinearLayout>

Iv. Additional Information

1, can also be optimized again, in the title bar plus a refresh button.

2, if the loading of the page need to download files, you need to set up the Setdownloadlistener method, according to the actual requirements of the project customization.

3, custom control is reproduced in, forget the source, thanks ~ ~

End

No advanced technology, practical on the line!

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.