Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6175745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:58:40+00:00 2026-05-23T23:58:40+00:00

I’m currently loading a webpage using a thread and I need a progress bar

  • 0

I’m currently loading a webpage using a thread and I need a progress bar to display while the page loads up. Whats the best way to implement this? My code to load the webpage is this

private Thread checkUpdate = new Thread() {

        @Override
        public void run() {
            try {
                /**
                 * establish a URL connection
                 */

                URL updateURL = new URL("http://www.mtsu.edu/alertupdates/");
                URLConnection conn = updateURL.openConnection();

                /**
                 * create an Input stream and buffered array to
                 * prepare for parsing.
                 */
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(50);

                /**
                 * read in the html and parse it to bytes.
                 */

                int current = 0;
                while((current = bis.read()) != -1){
                    baf.append((byte)current);
                }

                /**
                 *  Convert the Bytes read to a String. 
                 */

                html = new String(baf.toByteArray());
                int position = html.indexOf("<h1>");
                int position2 = html.indexOf("<!--",position);
                html = html.substring(position, position2);
                mHandler.post(showUpdate);
            } catch (Exception e){}
        }
    };

Here is my attempt at Using AsyncTask and the entire code of my project.

package com.MTSUAndroid;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Calendar;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import java.lang.String;
import android.content.Context;

public class Alerts extends Activity {
    /**
     * private variables to hold the html strings for parsing.
     */
    private String html = "";
    private Handler mHandler;
    private TextView text1;
    private TextView timestamp;
    private Button home;
    private Button refresh;
    private ProgressDialog myProgress;
    private int myProgressStatus = 0;

    private Handler myHandler = new Handler();


    /**
     * overriding on create with a new handler for threading
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alerts);
        initialControls();
        //mHandler = new Handler();
        //checkUpdate.start();
   }

   public void connectivityMessage(String msg){
        Context context = getApplicationContext();
        Toast toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setText(msg);
        toast.show();
   }

    /**
     * InitialControls function to set up all the initial controls for the GUI
     * Such as buttons, etc...
     */
    private void initialControls(){
    text1 = (TextView)findViewById(R.id.textView1);
    home = (Button)findViewById(R.id.home_button);
    //myProgress = (ProgressBar)findViewById(R.id.progressBar1);

        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        /**
         * TimeStamp for the alerts refresh button
         */
    timestamp = (TextView)findViewById(R.id.timestamp); 
    refresh = (Button)findViewById(R.id.update);

    /**
     * implementing the refresh button/loading the website
     */

    refresh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                /**
                 * establish a URL connection
                 */
                URL updateURL = new URL("http://www.mtsu.edu/alertupdates/");
                URLConnection conn = updateURL.openConnection();

                /**
                 * create an Input stream and buffered array to
                 * prepare for parsing.
                 */

                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(50);

                /**
                 * read in the html and parse it to bytes.
                 */

                int current = 0;
                while((current = bis.read()) != -1){
                    baf.append((byte)current);
                }

                /**
                 *  Convert the Bytes read to a String. 
                 */

                html = new String(baf.toByteArray());
                int position = html.indexOf("<h1>");
                int position2 = html.indexOf("<!--",position);
                html = html.substring(position, position2);
                mHandler.post(showUpdate);

                /**
                 * using calendar class for the refresh button
                 */

                Calendar c = Calendar.getInstance(); 
                int seconds = c.get(Calendar.SECOND);
                int minutes = c.get(Calendar.MINUTE);
                int hours = c.get(Calendar.HOUR);
                int years = c.get(Calendar.YEAR);
                int months = 1 + c.get(Calendar.MONTH);
                int days = c.get(Calendar.DAY_OF_MONTH);

                try{
                    if (c.get(Calendar.AM_PM) == 0)
                    {
                        String AM = "";
                        AM = "AM";
                        if (hours == 0)
                        {
                            hours = 12;
                        }
                        timestamp.setText("Refreshed on " + days + "-"
                        + months + "-" + years + " " +  hours + ":" + minutes + ":" + seconds + " " + AM);
                        timestamp.setTextSize(17f);
                        timestamp.setTextColor(Color.GREEN);
                    }
                    else
                    {
                        String PM = "";
                        PM = "PM";
                        timestamp.setText("Refreshed on " + days + "-"
                        + months + "-" + years + " " +  hours + ":" + minutes + ":" + seconds + " " + PM);
                        timestamp.setTextSize(17f);
                        timestamp.setTextColor(Color.GREEN);
                    }
                }
                catch (Exception e){}       
            }

            /**
             * Catch exception E to catch all errors.
             */
            catch (Exception e) {}
            }
        }
    );}

    /**
     * creating a new thread to run the URL.
     */
    private Thread checkUpdate = new Thread() {

        @Override
        public void run() {
            try {
                /**
                 * establish a URL connection
                 */

                URL updateURL = new URL("http://www.mtsu.edu/alertupdates/");
                URLConnection conn = updateURL.openConnection();

                /**
                 * create an Input stream and buffered array to
                 * prepare for parsing.
                 */
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(50);

                /**
                 * read in the html and parse it to bytes.
                 */

                int current = 0;
                while((current = bis.read()) != -1){
                    baf.append((byte)current);
                }

                /**
                 *  Convert the Bytes read to a String. 
                 */

                html = new String(baf.toByteArray());
                int position = html.indexOf("<h1>");
                int position2 = html.indexOf("<!--",position);
                html = html.substring(position, position2);
                mHandler.post(showUpdate);
            } catch (Exception e){}
        }
    };

    /**
     * set the textView to the freshly parsed html for viewing
     */

    private Runnable showUpdate = new Runnable(){
        @Override
        public void run(){
            text1.setText(Html.fromHtml(html));
        }
    };

    public class myTask extends AsyncTask<Void,Void,Void>{
        ProgressDialog progress;
        public myTask(ProgressDialog progress) {
            this.progress = progress;   
        }
        @Override
        protected void onPreExecute() {
            progress = ProgressDialog.show(Alerts.this, "Loading data..", "Please Wait");
            super.onPreExecute();
        }
        @Override
        protected Void doInBackground(Void... params) {
            try {
                /**
                 * establish a URL connection
                 */

                URL updateURL = new URL("http://www.mtsu.edu/alertupdates/");
                URLConnection conn = updateURL.openConnection();

                /**
                 * create an Input stream and buffered array to
                 * prepare for parsing.
                 */
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(50);

                /**
                 * read in the html and parse it to bytes.
                 */

                int current = 0;
                while((current = bis.read()) != -1){
                    baf.append((byte)current);
                }

                /**
                 *  Convert the Bytes read to a String. 
                 */

                html = new String(baf.toByteArray());
                int position = html.indexOf("<h1>");
                int position2 = html.indexOf("<!--",position);
                html = html.substring(position, position2);
                mHandler.post(showUpdate);
            } catch (Exception e){}
            return null;
        }
        protected void onPostExecute() {
            progress.dismiss();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:58:40+00:00Added an answer on May 23, 2026 at 11:58 pm

    An AsyncTask will help handle progress for you. Read here: http://developer.android.com/reference/android/os/AsyncTask.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm making a simple page using Google Maps API 3. My first. One marker
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.