Android WebView Sample code with a progress bar _android

Source: Internet
Author: User

Objective

If you do not use the system-titlebar (that is, the activity is set @android:style/theme.notitlebar), you need to write the progress bar yourself, which encapsulates a custom control and a public activity that loads the Web page for ease of use.

Body

One, screenshot

Two, custom controls

Copy Code/** * WebView * * * http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html with progress bar */@SuppressWarnings ("DEP

 Recation ") public class Progresswebview extends WebView {private ProgressBar ProgressBar;
  Public Progresswebview (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 ()); The public class Webchromeclient extends Android.webkit.WebChromeClient {@Override the public void onprogresschanged (WEBV
   Iew view, int newprogress) {if (newprogress = =) {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) p
  Rogressbar.getlayoutparams ();
  lp.x = l;
  LP.Y = t;
  PROGRESSBAR.SETLAYOUTPARAMS (LP);
 Super.onscrollchanged (L, T, OLDL, Oldt); Copy Code

Third, the public activity that loads the Web page

 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 Parameter URL = getintent (). Getstringextra ("url");

  Name = Getintent (). Getstringextra ("name");

  ~ ~ ~ Bound Control WebView = (Progresswebview) Findviewbyid (R.id.webview);
  ~ ~ ~ Set the data titletext.settext (name);
  Webview.getsettings (). Setjavascriptenabled (True); Webview.setdownloadlistener (New Downloadlistener () {@Override public void Ondownloadstart (string url, string Userag ENT, 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:o" rientation= "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. Supplementary Notes

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

2, if the loaded page has the need to download files, you need to set the Setdownloadlistener method, according to the actual needs of the project customization.

3, custom control is reproduced in, forget the source, thank you ~ ~

This article is the Android WebView with a progress bar example, there is a need to refer to the following.

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.