0

I would like to update the progressBar with Handler and for loop but without success.

Code:

public void increase_splash_bar (int from, int to)
{
    Handler handler1 = new Handler(Looper.getMainLooper()); 
    for (progress_k = from; progress_k<=to ;progress_k++) 
    {
        handler1.postDelayed(new Runnable() 
        {
             @Override
             public void run() 
             {
                  FrontLayout.update_splash_progress_bar(progress_k, 100);
             }
        }, 2000);
    } 
}

Question:

The progress bar increase immediately to the end value instead of progressively. Why?

pearmak
  • 4,979
  • 15
  • 64
  • 122
  • You can update progress bar continuously with a handler. Why do you need a for loop for? – Srikar Reddy Feb 25 '17 at 06:38
  • could you please advise explicitly? Or base on code above, why it does not work properly for increasing the progressbar gradually, but directly jump to the end value? – pearmak Feb 25 '17 at 06:48

3 Answers3

3

Try this:

public void increase_splash_bar (int from, int to)
{
    Handler handler1 = new Handler(Looper.getMainLooper()); 
    for (progress_k = from; progress_k<=to ;progress_k++) 
    {
        final int curr_progress_k = progress_k;
        handler1.postDelayed(new Runnable() 
        {
            @Override
            public void run() 
            {
                 FrontLayout.update_splash_progress_bar(curr_progress_k, 100);
            }
        }, progress_k * 100);      // adjust "100" value to adjust speed
    } 
}
Joel Duggan
  • 215
  • 1
  • 7
  • Yet changing to this progress_k * 100 still cannot fix the problem that the value directly jumping to end immediately? – pearmak Feb 25 '17 at 06:47
  • 1
    its not gonna work, your `Runnable`s will be delaeyd but still the value passed to `update_splash_progress_bar` method will be equal to `to + 1` (it will not be `from, from + 1, from + 2, ... , to`) – pskink Feb 25 '17 at 06:49
  • @pearmak simply use `Message`s (and override `Handler#handleMessage`), not `Runnable`s – pskink Feb 25 '17 at 06:53
  • @pearmak or if you really, really need `Runnable` add a field like this in your `Runnable`: `int field_progress_k = progress_k;` and use that `field_progress_k` field – pskink Feb 25 '17 at 07:09
  • @pearmak I updated the answer to fix the problem you stated. The `final int curr_progress_k` is important, and I forgot about it. – Joel Duggan Feb 25 '17 at 15:58
  • yes, this is what i stated in my prev comment but the right way is either to use not anonymous `Runnable` (but rther a class that extends `Runnable` and pass current loop index to the constructor) or (what i would do) use `Message`s and custom `Handler` class – pskink Feb 25 '17 at 16:38
  • yes, worse and better, the way OP want to do that is among the former – pskink Feb 25 '17 at 17:37
3

Repeat a task with a time delay? @inazaruk

private ProgressBar progressBar;
private Handler mHandler;
private int progressInt = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    progressBar = (ProgressBar) findViewById(R.id.pb);
    progressBar.setProgress(0);
    mHandler = new Handler();
    runnable.run();

}


Runnable runnable = new Runnable() {
    @Override
    public void run() {
        try {
            updateProgress();
        } catch (Exception ignored) {

        } finally {
            mHandler.postDelayed(runnable, progressInt);
        }
    }
};

private void updateProgress() {
    progressInt += 1;
    if (progressInt > 100) {
        mHandler.removeCallbacks(runnable);
    } else {
        progressBar.setProgress(progressInt);
    }
}
Community
  • 1
  • 1
Ninja
  • 366
  • 2
  • 7
0

try this code:

Solution 1

public void increase_splash_bar (int from, int to)
{
    Handler handler1 = new Handler(); 
    class Task implements Runnable {

        int start,end;

        Task(int a,int b) { start = a; end = b;}

        @Override
        public void run() {
            for (int i =start ; i <= end; i++) {
                final int value = i;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler1.post(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.setProgress(value);
                    }
                });
            }
        }
    }

    Thread t = new Thread(new Task(from, to));   //call it
    t.start();

}

Solution 2: More Simple

If thread is too much to ask for this problem..

you can use the following solution to use a single Handler to update progressbar:

code

 public class HandlerDemo extends Activity
{
  ProgressBar bar;
  Handler handler = new Handler()
  {
    @Override
    public void handleMessage(Message msg)
    {
      bar.incrementProgressBy(5);
    }
  };
  boolean isRunning = false;

  @Override
  public void onCreate(Bundle icicle)
  {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    bar = (ProgressBar) findViewById(R.id.progress);
  }

  public void onStart()
  {
    super.onStart();
    bar.setProgress(0);

    Thread background = new Thread(new Runnable()
    {
      public void run()
      {
        try
        {
          for (int i = 0; i < 20 && isRunning; i++)
          {
            Thread.sleep(1000);
            handler.sendMessage(handler.obtainMessage());
          }
        }
        catch (Throwable t)
        {
          // just end the background thread
        }
      }
    });

    isRunning = true;
    background.start();
  }

  public void onStop()
  {
    super.onStop();
    isRunning = false;
  }
}

Hope it helps..

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
  • a new `Thread`? for making such a simple animation? it seems you dont know what you can do with simple `Handler` – pskink Feb 25 '17 at 08:11
  • sure i do know...i am open to take any suggestion.There are many ways to solve a `SIMPLE` problem – rafsanahmad007 Feb 25 '17 at 08:17
  • and the best one is to use simplest solutions and not to use a cannon to kill a fly - *"Everything Should Be Made as Simple as Possible, But Not Simpler"* – pskink Feb 25 '17 at 08:24
  • i get it ...thankx i will look into other simple ways to solve the problem. – rafsanahmad007 Feb 25 '17 at 08:27
  • i have edited the answer...*Stop downVoting the answer without a valid Reason* add a comment what is the problem! – rafsanahmad007 Feb 26 '17 at 09:07
  • you are still using a `new Thread`, this is NOT a solution for any animation on android platform, see "delayed" methods of a `Handler` instead or for example Joel Duggan's answer here – pskink Feb 26 '17 at 14:21