progress bar not showing progress on bar

I'm using progress bar but it is not showing contineous progress. It gives a green bar and does not show continuous progress (i.e. 10 times progress).

private ProgressBar mProgress;
private int mProgressStatus = 0;

private Handler mHandler = new Handler();

protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.fetchee_distance);
    mProgress = (ProgressBar) findViewById(R.id.p);

    Thread timer = new Thread() {
        public void run() {
            try {
                sleep(1000);
                while (mProgressStatus < 100) {
                    mProgress.setProgress(mProgressStatus);
                    // mProgress.setMax(100);
                    mProgressStatus += 10;

                    System.out.println("count" + mProgressStatus);

                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                /*
                 * Intent openMainList = new Intent(StartPoint.this,
                 * in.isuru.caf.MainList.class);
                 * startActivity(openMainList);
                 */
            }
        }
    };
    timer.start();
}

Upvotes: 1

Views: 1379

Answers (1)

QVDev
QVDev

Reputation: 1093

Moved sleep thread in the loop, in you're code it first sleeps and then goes in the while loop where it just goes really quick through the iteration and results in directly showing the full 100% of the bar, instead of sleeping.

private ProgressBar mProgress; private int mProgressStatus = 0;

private Handler mHandler = new Handler();

protected void onCreate(Bundle icicle) { super.onCreate(icicle);

setContentView(R.layout.fetchee_distance);
mProgress = (ProgressBar) findViewById(R.id.p);

Thread timer = new Thread() {
    public void run() {
        try {

            while (mProgressStatus < 100) {
                  sleep(1000);//Sleep moved to while thread
                mProgress.setProgress(mProgressStatus);
                // mProgress.setMax(100);
                mProgressStatus += 10;

                System.out.println("count" + mProgressStatus);

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            /*
             * Intent openMainList = new Intent(StartPoint.this,
             * in.isuru.caf.MainList.class);
             * startActivity(openMainList);
             */
        }
    }
};
timer.start(); }

Upvotes: 1

Related Questions