Recommended reading:Detailed explanation of Android WebView linear progress bar example
Recently, I used webview to nest a lottery webpage in the Android project. The event was launched and runs well (N requirements and sudden bugs were changed). Fortunately, this mode of activity only requires modifying the webpage and not repackaging and publishing the market. This is also one of the advantages of this mode development. Later, according to the feedback from Product Brother, there was no progress prompt for loading the web page. Well, I didn’t think so much about this at that time. I had to add this... Of course, I thought it was easy to handle it... In fact, it was more complicated than easy...
1. First customize a WebView control
/** * Webivew with progress bar * @author [email protected] */ @SuppressWarnings("deprecation") public class ProgressWebView extends WebView { private final static String TAG = (); private ProgressBar progressBar; private Context context; public ProgressWebView(Context context, AttributeSet attrs) { super(context, attrs); = context; progressBar = new ProgressBar(context, null, ); (new (.MATCH_PARENT, , , )); (getResources().getDrawable(.wevbview_progressbar)); addView(progressBar); setWebChromeClient(new WebChromeClient()); } public class WebChromeClient extends { @Override public void onProgressChanged(WebView view, int newProgress) { (TAG, "newProgress" + newProgress); if (newProgress == ) { (GONE); } else { if (() == GONE) (VISIBLE); (newProgress); } (view, newProgress); } // Handle in javascript@Override public boolean onConsoleMessage(ConsoleMessage cm){ (TAG, "webview console " + () + " of " + () + " : " + ()); return true; } // Handle alert() in javascript@Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { (context, message, Toast.LENGTH_SHORT, ); (); return true; } } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { LayoutParams lp = (LayoutParams) (); = l; = t; (lp); (l, t, oldl, oldt); } }
2. Introduce this control in the layout file that needs to use the webview
< android: android:layout_width="match_parent" android:layout_height="match_parent" />
3. Add a drawable file and modify the progress bar style controlled by progress
<?xml version="." encoding="utf-"?> <layer-list xmlns:andro > <!-- background --> <item android:> <shape> <solid android:color="@color/default_bg" /> </shape> </item> <!-- Progress bar --> <item android:> <clip> <shape> <solid android:color="#EAE" /> </shape> </clip> </item> </layer-list>
4. Use this control's related code in activity or fragment
ProgressWebView webView = (ProgressWebView)findViewById(.them_webview); (new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { if (url != null && ("http://")) startActivity(new Intent(Intent.ACTION_VIEW, (url))); } }); ("Web url");
Through the above code, we have realized the relevant functions of adding web page loading progress bars in Webview. I hope it will be helpful to everyone.