Add beautiful loading progress bars and WebView loading progress bars to webview

Source: Internet
Author: User

Add beautiful loading progress bars and WebView loading progress bars to webview

To enhance the user experience, all the progress bars added to the WebView header look good.


Layout XMl: activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent">        <WebView            android:id="@+id/webView"            android:layout_width="fill_parent"            android:layout_height="fill_parent"/>                <ProgressBar          android:id="@+id/pb"          style="?android:attr/progressBarStyleHorizontal"          android:layout_width="fill_parent"          android:layout_height="3dip"          android:indeterminateOnly="false"          android:max="100"          android:progressDrawable="@drawable/progress_bar_states" >      </ProgressBar></RelativeLayout>

Custom progress bar:

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

Then there is the main code of the Activity:

ProgressBar pb;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.xxx);pb = (ProgressBar) findViewById(R.id.pb);pb.setMax(100);WebView webView = (WebView) findViewById(R.id.webview);webView.getSettings().setJavaScriptEnabled(true);webView.getSettings().setSupportZoom(true);  webView.getSettings().setBuiltInZoomControls(true);webView.setWebChromeClient(new WebViewClient() );webView.loadUrl("http://www.x.com");}private class WebViewClient extends WebChromeClient {@Overridepublic void onProgressChanged(WebView view, int newProgress) {pb.setProgress(newProgress);if(newProgress==100){pb.setVisibility(View.GONE);}super.onProgressChanged(view, newProgress);}}

Not to mention, I know that everyone is most concerned about the effect:

Reprinted, please note: jixiao blog» [original] add beautiful loading progress bars to WebView

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.