How to make a delay that can be seen? I need to move the seekbar automatically when loading the activation, and I need to see the process of movement. I tried:

for(int i = 0; i < 10; i++) { try { Thread.sleep(600); seekBar.setProgress(i+7); } catch (InterruptedException e) { e.printStackTrace(); } } 

And the process of movement jumps over, that is, it is performed, but the process is not visible.

  • for(int i = 0; i < 200; i++) { try { Thread.sleep(50); seekBar.setProgress(i+0.35); } catch (InterruptedException e) { e.printStackTrace(); } } for(int i = 0; i < 200; i++) { try { Thread.sleep(50); seekBar.setProgress(i+0.35); } catch (InterruptedException e) { e.printStackTrace(); } } (in short, just increase the number of steps) // most likely there are ready-made solutions and this is a crutch - Nikita Gordeyev
  • Are you blocking the UI thread? I have bad news for you. - VladD
  • I agree that at this time the UI thread is blocked, so I ask how to execute it or what? Is it possible to create some second process? - J. Defenses

1 answer 1

Something like this:

  new Thread() { @Override public void run() { for(int i = 0; i < 10; i++) { try { Thread.sleep(600); } catch (InterruptedException e) { e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { seekBar.setProgress(i + 7); } }); } } }.start(); 

The compilability of the code is not guaranteed, the visibility of variables must also be resolved, but the idea should be clear.

  • It is necessary to declare the class beyond the bounds: private static SeekBar seekBar; private static int i = 0; And in the constructor, initialize seekBar and everything works. - J. Defenses
  • static SeekBar is a bad idea; context leaks may be present. - Eugene Krivenja
  • The loop variable is also not necessary to endure. seekBar.setProgress(seekBar.getProgress() + 7); as an option. - Eugene Krivenja
  • and how best to declare SeekBar, if static is a bad option? - J. Defenses
  • Standardly, as a variable of an activit object or a fragment. - Eugene Krivenja