WebView linear progress bar, webview linear

Source: Internet
Author: User

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" />

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.