Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Wing Chow
Wing Chow
845 Points

Question on adding progress bar codes on blogwebview (blogreaderapp) HELP!

Dear all:

I am trying to add the progress bar codes on the BlogWebViewActivity.java However, no matter how slow the webview is load. The progress bar never read pop out.

here is my code.

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_web_view);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
        mProgressBar.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        Uri blogUri = intent.getData();
        mUrl = blogUri.toString();
        WebView webView = (WebView) findViewById(R.id.webView1);
        webView.loadUrl(mUrl);
        mProgressBar.setVisibility(View.INVISIBLE);
    }

I am wondering if this is cause by the fact that the webpage is still loading after the mProgressBar.setVisibility(View.INVISIBLE) have already execute.

If yes, how should i modify it to make it to work? Thank you very much!

Wayne

1 Answer

Wing Chow
Wing Chow
845 Points

Hi guys. I manage to pull this out by using the technique in the following link: http://www.technotalkative.com/android-load-webview-with-progressbar/

And here is my modification for it

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_web_view);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
        mProgressBar.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        Uri blogUri = intent.getData();
        mUrl = blogUri.toString();
        WebView webView = (WebView) findViewById(R.id.webView1);
        webView.setWebViewClient(new mWebClient());
        webView.loadUrl(mUrl);
    }

    public class mWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;        
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);    
            mProgressBar.setVisibility(View.GONE);
        }
    }

If anyone could confirm my code, that would be awesome!

Thank you very much!