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

  • Home
  • SEARCH
  • 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 8198117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:53:01+00:00 2026-06-07T05:53:01+00:00

My activity have a ProgressBar. When start activity, I’ll check value get from another

  • 0

My activity have a ProgressBar. When start activity, I’ll check value get from another place and update to ProgressBar. Here’s my code:

final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar_detail);
final TextView progressText = (TextView) findViewById(R.id.progressText_detail);
final ImageView btnCancel = (ImageView) findViewById(R.id.imgCancel_detail);
progressBar.setVisibility(View.VISIBLE);
progressText.setVisibility(View.VISIBLE);
btnCancel.setVisibility(View.VISIBLE);
Thread t = new Thread() {
    public void run() {
        ((Activity) ctx).runOnUiThread(new Runnable() {
            public void run() {
                int oldProgress = 0;
                while (progressBar.getProgress() < 100) {
                    int value = Downloader.progress.get(gameId+ "");
                    if (value != oldProgress) {
                        oldProgress = value;
                        progressBar.setProgress(value);
                        progressText.setText(value + " %");
                    }
                }
            }
        });
    }
};
t.start();

Value of ProgressBar i get from int value = Downloader.progress.get(gameId) and it’s correct. But when I run this code, the activity is not responding and not showing anything (but app not crash). Seems like the thread to update ProgressBar running and block the UI thread so the activity layout is not showing.

What’s wrong with my code? What is the correct way to update ProgressBar in this situation?

  • 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-06-07T05:53:02+00:00Added an answer on June 7, 2026 at 5:53 am

    You are running a while (progressBar.getProgress() < 100) in the UI thread (you use runOnUiThread) so UI thread will be blocked (and nothing painted) until this loop finish.

    You can:

    • Put the runOnUiThread inside the loop and, maybe, a sleep inside de runOnUiThread call. However, it is not the preferred kind of solution since you are using too much CPU to just look for a value change. The sleep approach it a bit ugly but solves the cpu problem. It would be something like::

      Thread t = new Thread() {
          public void run() {
          int oldProgress = 0;
      
          while (progressBar.getProgress() < 100) {
                  ((Activity) ctx).runOnUiThread(new Runnable() {
                      public void run() {
                          int value = Downloader.progress.get(gameId+ "");
                          if (value != oldProgress) {
                              oldProgress = value;
                              progressBar.setProgress(value);
                              progressText.setText(value + " %");
                          }
                      }
                  }
                  Thread.sleep(500);
              }
          });
        }
      };
      
    • Update the progress bar in an event way so you only call (in the UI thread) the update code when progress changes. It is the preferred way.

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

Sidebar

Related Questions

Having trouble to start a new activity after progressbar have loaded. Im getting stucked
I have this code: public class Chronom extends Activity { Chronometer mChronometer; ProgressBar progre;
I have an android activity that downloads content from the web. The activity have
Within one Activity I have th following piece of code: public void onStartMonitoringToggleClicked(View v)
I am developing an activity which have an Update button as menu for android.
What i have here is a simple webview activity that when loaded it auto
I have the following code in Main.java : public class Main extends Activity implements
I have to an Activity in which I have to update the progress of
I am building up a UI from code and can successfully add ProgressBar widgets,
In Android, from Activity A, if I start new Activity B, Android will automatically

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.