WebView linear progress bar, webview linear
Reference: http://www.cnblogs.com/hubli/p/4835549.html
When the APP jumps to the web page, see: http://blog.csdn.net/raphael55/article/details/6975918
:
1. wevbview_progressbar.xml
<Layer-list xmlns: android = "http://schemas.android.com/apk/res/android">
<! -- Background -->
<Item android: id = "@ android: id/background">
<Shape>
<Solid android: color = "@ android: color/transparent"/>
</Shape>
</Item>
<! -- Progress bar -->
<Item android: id = "@ android: id/progress">
<Clip>
<Shape>
<Solid android: color = "@ android: color/holo_green_light"/>
</Shape>
</Clip>
</Item>
</Layer-list>
2. ProgressWebView. java
@ SuppressWarnings ("deprecation ")
Public class ProgressWebView extends WebView {
Private final static String TAG = ProgressWebView. class. getSimpleName ();
Private ProgressBar progressBar;
Private Context context;
Public ProgressWebView (Context context, AttributeSet attrs ){
Super (context, attrs );
This. context = context;
ProgressBar = new ProgressBar (context, null, android. R. attr. progressBarStyleHorizontal );
ProgressBar. setLayoutParams (new AbsoluteLayout. LayoutParams (AbsoluteLayout. LayoutParams. MATCH_PARENT, 3, 0, 0 ));
ProgressBar. setProgressDrawable (getResources (). getDrawable (R. drawable. wevbview_progressbar ));
AddView (progressBar );
SetWebChromeClient (new WebChromeClient ());
}
Public class WebChromeClient extends android. webkit. WebChromeClient {
@ Override
Public void onProgressChanged (WebView view, int newProgress ){
Log. d (TAG, "newProgress" + newProgress );
If (newProgress = 100 ){
ProgressBar. setVisibility (GONE );
} Else {
If (progressBar. getVisibility () = GONE)
ProgressBar. setVisibility (VISIBLE );
ProgressBar. setProgress (newProgress );
}
Super. onProgressChanged (view, newProgress );
}
// Process console. log in javascript
@ Override
Public boolean onConsoleMessage (ConsoleMessage cm ){
Android. util. log. d (TAG, "webview console" + cm. lineNumber () + "of" + cm. sourceId () + ":" + cm. message ());
Return true;
}
// Process alert () in javascript ()
@ Override
Public boolean onJsAlert (WebView view, String url, String message, JsResult result ){
Result. cancel ();
Return true;
}
}
@ 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 );
}
}
3. MainActivity. java
ProgressWebView webView = (ProgressWebView)findViewById(R.id.them_webview);
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("http://www.cnblogs.com/hubli/p/4835549.html");
4. activity_main.xml
<com.etoury.webviewprogress.ProgressWebView
android:id="@+id/them_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />