praxmon
praxmon

Reputation: 5131

ProgressBar in android fragment

I have a progressbar in a webview inside a fragment. Inside the onViewCreated() method I have:

ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);

The progressbar is defined as follows:

<ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        />

And to show the progress of loading the page I have the following code:

webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
            }
        });

And I am loading the url: "http://www.techcrunch.com"

The problem is, while the page is loading the progressbar isn't visible. So to debug this I tried to output the progress inside the onProgressChanged() method. Where am I going wrong?

I have used this link and this link as a reference but no joy.

Upvotes: 0

Views: 1372

Answers (1)

Htoo Aung Hlaing
Htoo Aung Hlaing

Reputation: 2263

Custom Progress Drawable

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Define the background properties like color etc -->
    <item android:id="@android:id/background">
        <shape>
            <gradient
                    android:startColor="#000001"
                    android:centerColor="#0b131e"
                    android:centerY="1.0"
                    android:endColor="#0d1522"
                    android:angle="270"
            />
        </shape>
    </item>

 <!-- Define the progress properties like start color, end color etc -->
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#007A00"
                    android:centerColor="#007A00"
                    android:centerY="1.0"
                    android:endColor="#06101d"
                    android:angle="270"
                />
            </shape>
        </clip>
    </item>

</layer-list>

Init your progressbar and webview

webView = (WebView) rootView.findViewById(R.id.webView);
 webView.setWebChromeClient(new MyWebViewClient());
 webView.getSettings().setJavaScriptEnabled(true);
 webView.loadUrl(url);

mprogressbar = (ProgressBar) rootView.findViewById(R.id.progressBar);

 Drawable customDrawable= getResources().getDrawable(R.drawable.custom_progressbar);

 mprogressbar.setProgressDrawable(customDrawable);

And then, set progress in WebViewClient,

private class MyWebViewClient extends WebChromeClient { 
        @Override
        public void onProgressChanged(WebView view, int newProgress) {          
            setValue(newProgress);
            super.onProgressChanged(view, newProgress);
        }
    }



    public void setValue(int progress) {
        this.mprogressbar.setProgress(progress);

        if(progress >= 100)
        {
            mprogressbar.setVisibility(View.INVISIBLE);
        }
    }

Upvotes: 1

Related Questions