Customize the WebView with a progress bar and add the webviewweb header and url retrieval function.

Source: Internet
Author: User

Customize the WebView with a progress bar and add the webviewweb header and url retrieval function.

1. Custom ProgressWebView

Package com. app. android05; import android. content. context; import android. graphics. bitmap; import android. util. attributeSet; import android. webkit. webView; import android. webkit. webViewClient; import android. widget. progressBar;/*** @ author admin * WebView with progress bar */public class ProgressWebView extends WebView {private Context context Context; private ProgressBar progressbar; private OnWebCallBack onWebCallBack; // Callback public ProgressWebView (Context context) {this (context, null);} public ProgressWebView (Context context, AttributeSet attrs) {this (context, attrs, android. r. attr. webTextViewStyle);} public ProgressWebView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); this. context = context; init (); setWebViewClient (new MyWebViewClient (); setWebCh RomeClient (new WebChromeClient ();}/*** set ProgressBar */void init () {progressbar = new ProgressBar (context, null, android. r. attr. progressBarStyleHorizontal); progressbar. setLayoutParams (new LayoutParams (LayoutParams. MATCH_PARENT, 20, 0, 0); addView (progressbar);} public class WebChromeClient extends android. webkit. webChromeClient {@ Override public void onProgressChanged (WebView View, int newProgress) {if (newProgress = 100) {progressbar. setVisibility (GONE);} else {progressbar. setVisibility (VISIBLE); progressbar. setProgress (newProgress);} super. onProgressChanged (view, newProgress) ;}@ Override public void onReceivedTitle (WebView view, String title) {super. onwebedtitle (view, title); if (onWebCallBack! = Null) {// obtain the title onWebCallBack. getTitle (title) ;}}/ *** if not overwritten, will jump to the mobile browser * @ author admin */public class MyWebViewClient extends WebViewClient {@ Override public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {// Handle the goBack () ;}@ Override public boolean shouldOverrideUrlLoading (WebView view, String url) {view. loadUrl (url); return true;} @ Ov Erride public void onPageFinished (WebView view, String url) {super. onPageFinished (view, url) ;}@ Override public void onPageStarted (WebView view, String url, Bitmap favicon) {if (onWebCallBack! = Null) {// obtain the WebView address onWebCallBack. getUrl (url) ;}}@ 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);}/*** sets the WebView logoff * @ param onWebCallBack */void setOnWebCallBack (OnWebCallBack onWebCallBack) {this. onWebCallBack = onWebCallBack;} interface OnWebCallBack {/*** obtain the title * @ param title */void getTitle (String title ); /*** obtain the WebView address * @ param url */void getUrl (String url );}

 

2. xml reference

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.app.android05.MainActivity$PlaceholderFragment" >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="title" />        <TextView        android:id="@+id/url"        android:layout_below="@id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="url" />    <com.app.android05.ProgressWebView        android:id="@+id/web"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@id/url" /></RelativeLayout>


3. MainActivity call

Package com. app. android05; import android. app. activity; import android. OS. bundle; import android. widget. textView; public class MainActivity extends Activity {private TextView title_ TV; private TextView url_ TV; private ProgressWebView webView; private String url = "http://dict.youdao.com/"; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); title_ TV = (TextView) findViewById (R. id. TV); url_ TV = (TextView) findViewById (R. id. url); webView = (ProgressWebView) findViewById (R. id. web); // sets the webview return function to obtain the Url webView. setOnWebCallBack (new OnWebCallBack () {// obtain the title @ Override public void getTitle (String title) {title_ TV .setText (title );} // obtain the current web URL @ Override public void getUrl (String url) {url_ TV .setText (url) ;}}); webView. loadUrl (url );}}

 


 

 

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.