user3186489
user3186489

Reputation: 1

Why Is My Progress Bar Not Working?

package com.example.progressdialog;

import com.example.progressdialog.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

   private ProgressDialog progress;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      progress = new ProgressDialog(this);
   }


   public void open(View view){
      progress.setMessage("Start This Baby Up!");
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(false);
      progress.show();

   final int totalProgressTime = 100;

   final Thread t = new Thread(){

   @Override
   public void run(){

      int jumpTime = 0;
      while(jumpTime < totalProgressTime){
         try {
            sleep(500);
            jumpTime += 1;
            progress.setProgress(0);
         } catch (InterruptedException e) {


           e.printStackTrace();
         }

      }

   }
   };
   t.start();

   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

I cannot seem to get my progress bar to actually progress forward. It simply displays a static 0/100 bar. I am trying to get it to move forward in a smooth manner taking approximately 30-45 seconds to complete. Could someone tell me what I am doing wrong? I am very new to java! Thanks!

Upvotes: 0

Views: 110

Answers (1)

Carsten Hoffmann
Carsten Hoffmann

Reputation: 941

Citation:

progress.setProgress(0);

this should be

progress.setProgress(jumpTime);

I suppose?

Also I guess, this will lead to a problem. You cannot access UI-Components from background Threads. You need to use a Handler. See this Example.

Upvotes: 1

Related Questions