WebView usage and addition progress bar, webview addition progress bar

Source: Internet
Author: User

WebView usage and addition progress bar, webview addition progress bar

The effect is similar to opening a webpage. a progress bar is displayed on the header to show the loading progress.

1. Load a webpage on Android

webView.loadUrl(urlString);

2. display the page loading and loading progress. However, sometimes onPageStarted and other calls may occur, which may be a webpage issue and cannot be solved.

@ Override public void onPageStarted (WebView view, String url, Bitmap favicon) {// TODO Auto-generated method stub super. onPageStarted (view, url, favicon); System. out. println ("Page START" + url + "" + favicon) ;}@ Override public void onPageFinished (WebView view, String url) {// TODO Auto-generated method stub super. onPageFinished (view, url); System. out. println ("Page ended" + url );}
WebView. setWebChromeClient (new WebChromeClient () {@ Override public void onProgressChanged (WebView view, int newProgress) {// TODO Auto-generated method stub super. onProgressChanged (view, newProgress); Log. d ("jiejie", "ProgressChanged ++" + newProgress); if (newProgress = 100) {progressBar. setVisibility (View. GONE); // progressBar. setProgress (newProgress);} else {progressBar. setVisibility (View. VISIBLE); progressBar. setProgress (newProgress); // set the loading progress }}});

3. overwrite the background color progressDrawable of ProgressBar, which must be implemented using layer-list.

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:id="@android:id/background">        <shape>            <corners android:radius="2dp" />            <gradient                android:angle="270"                android:centerColor="#E3E3E3"                android:endColor="#E6E6E6"                android:startColor="#C8C8C8" />        </shape>    </item>    <item android:id="@android:id/progress">        <clip>            <shape>                <corners android:radius="2dp" />                <gradient                    android:centerColor="#4AEA2F"                    android:endColor="#31CE15"                    android:startColor="#5FEC46" />                            </shape>        </clip>    </item></layer-list>
View Code

4. Introduction to common WebSettings Methods

SetJavaScriptEnabled (true); // supports jssetPluginsEnabled (true); // supports the plugin setUseWideViewPort (false); // adjusts the image size to the appropriate webview. setsuppzoom zoom (true ); // supports setLayoutAlgorithm Scaling (LayoutAlgorithm. SINGLE_COLUMN); // supports relayout of supportMultipleWindows (); // multiple-window setCacheMode (WebSettings. LOAD_CACHE_ELSE_NETWORK); // disable setAllowFileAccess (true) in the webview cache; // you can set setNeedInitialFocus (true) to access the file; // when webview calls requestFocus, webview webSettings is set for the webview node. setBuiltInZoomControls (true); // you can set the support for Zoom (true); // you can use JS to open a new window. setLoadWithOverviewMode (true); // setLoadsImagesAutomatically (true ); // supports automatic image loading

5. Full solution of WebViewClient Method

DoUpdateVisitedHistory (WebView view, String url, boolean isReload) // (update history) onFormResubmission (WebView view, Message dontResend, Message resend) // (the application re-Requests webpage data) onLoadResource (WebView view, String url) // It is called when page resources are loaded. Each Resource (slice) is called once. OnPageStarted (WebView view, String url, Bitmap favicon) // This event is called to start loading the page. Generally, we can set a loading page here to tell the user program to wait for the network response. OnPageFinished (WebView view, String url) // call when the page load ends. Similarly, we know that the loading of a page is complete, so we can close the loading entry and switch the program action. OnReceivedError (WebView view, int errorCode, String description, String failingUrl) // (report error message) onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm) // (obtain the authorization request for returned information) onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) // rewrite this method to allow webview to process https requests. OnScaleChanged (WebView view, float oldScale, float newScale) // (called when the WebView changes) onUnhandledKeyEvent (WebView view, KeyEvent event) // (called when the Key event is not loaded) shouldOverrideKeyEvent (WebView view, KeyEvent event) // rewrite this method to handle button events in the browser. ShouldOverrideUrlLoading (WebView view, String url) // It is called only when the requested link is clicked. If this method is rewritten, true is returned, indicating whether to click the link in the webpage or jump to the current webview, do not jump to the browser. We can perform many operations on this function. For example, if we read some special URLs, we can cancel the operation without opening the address and perform other pre-defined operations, this is very necessary for a program.

6. Main program code

1 package com. item. jiejie. activity; 2 3 import com. item. jiejie. r; 4 5 import android. annotation. suppressLint; 6 import android. app. activity; 7 import android. graphics. bitmap; 8 import android. OS. bundle; 9 import android. util. log; 10 import android. view. keyEvent; 11 import android. view. view; 12 import android. webkit. webChromeClient; 13 import android. webkit. webSettings; 14 import android. webkit. webView; 15 import android. webkit. webViewClient; 16 import android. widget. progressBar; 17 18/** 19 * Add a type progress bar 20 * @ author Administrator 21*22 */23 @ SuppressLint ("SetJavaScriptEnabled") when loading a WebView ") 24 public class WebViewActivity extends Activity {25 private WebView webView; 26 private ProgressBar progressBar; 27 private String urlString = "http: // 61.156.45.47: 8081/dtmsapp/sy.html "; 28 @ Override 29 protected void onCreate (Bundle savedInstanceState) {30 // TODO Auto-generated method stub 31 super. onCreate (savedInstanceState); 32 setContentView (R. layout. actvity_webview); 33 initView (); 34} 35 private void initView () {36 // TODO Auto-generated method stub 37 webView = (WebView) findViewById (R. id. webview); 38 progressBar = (ProgressBar) findViewById (R. id. pergress); 39 WebSettings webSettings = webView. getSettings (); 40 webSettings. setJavaScriptEnabled (true); 41 webSettings. setUseWideViewPort (true); 42 webSettings. setLoadWithOverviewMode (true); 43 webSettings. setdefatextextencodingname ("UTF-8"); 44 webSettings. setCacheMode (WebSettings. LOAD_NO_CACHE); // disable the WebView cache 45 webView. setWebViewClient (new WebViewClient () {46 @ Override 47 public void onPageStarted (WebView view, String url, Bitmap favicon) {48 // TODO Auto-generated method stub 49 super. onPageStarted (view, url, favicon); 50 System. out. println ("Page START" + url + "" + favicon); 51} 52 53 @ Override 54 public void onPageFinished (WebView view, String url) {55 // TODO Auto-generated method stub 56 super. onPageFinished (view, url); 57 System. out. println ("Page end" + url); 58} 59 @ Override 60 public boolean shouldOverrideUrlLoading (WebView view, String url) {61 // TODO Auto-generated method stub 62 // return super. shouldOverrideUrlLoading (view, url); 63 view. loadUrl (url); 64 return true; 65} 66}); 67 webView. setWebChromeClient (new WebChromeClient () {68 @ Override 69 public void onProgressChanged (WebView view, int newProgress) {70 // TODO Auto-generated method stub 71 super. onProgressChanged (view, newProgress); 72 Log. d ("jiejie", "ProgressChanged ++" + newProgress); 73 if (newProgress = 100) {74 progressBar. setVisibility (View. GONE); 75 // progressBar. setProgress (newProgress); 76} else {77 progressBar. setVisibility (View. VISIBLE); 78 progressBar. setProgress (newProgress); // set the loading progress to 79} 80} 81}); 82 webView. loadUrl (urlString); 83} 84 85 // sets the return key action (to prevent the program from exiting directly by pressing the return key) 86 @ Override 87 public boolean onKeyDown (int keyCode, KeyEvent event) {88 // The method stub automatically generated by TODO 89 if (keyCode = KeyEvent. KEYCODE_BACK) {90 if (webView. canGoBack () {// when the webview is not on the first page, 91 webView is returned. goBack (); 92 return true; 93} 94 // else {// when webview is on the first page, exit program 95 // System. exit (0); 96 //} 97 98 99} 100 return super. onKeyDown (keyCode, event); 101} 102}

 

  

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.