It is necessary that the text be automatically displayed in the TextView , and after a pause it has changed. I have so:

 ... tv.setText("1"); SystemClock.sleep(2000); tv.setText("2"); ... 

But the result is only this: Pause and deduce tv.setText("2"); , but tv.setText("1"); - not at all. I changed the types of pauses, the types of actions before and after it, the types of methods (by click, by redirection ...), but alas, he seemed to ignore the first action. This is probably only related to widgets.

  • one
    Judging by the use of the SystemClock class, you need to add the android tag to the question. - Mikhail Grebenev
  • All the answers are good. - Andrei Andrei Pedash

3 answers 3

Use Handler.

 ... tv.setText("1"); runWithDelay(); ... private void runWithDelay() { final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { tv.setText("2"); } }, 2000); } 
  • The way works, View is wonderfully updated, thanks. - Andrei Andrei Pedash

SystemClock.sleep() not recommended for use in the main thread.

As an option to use:

 Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { // что-то } }, 2000); 

or

 new CountDownTimer(5000, 1000) { @Override public void onTick(long millisUntilFinished) { // тик } @Override public void onFinish() { // на финише } }.start(); 
  • Yes, now TextView is being updated. A very interesting way to CountDownTimer, never seen, thanks! - Andrei Andrei Pedash

You do not need to put down the main stream - it is because of this and can not update the text. You can do it through a Handler or so (each View has its own):

 tv.setText("1"); tv.postDelayed(() -> tv.setText("2"), 2000); 
  • Without a hint, I would never have guessed, now everything is working, thanks! The only thing I did not understand "every View has its own" Handler? - Andrei Andrei Pedash
  • one
    More specifically, the reference to Handler , to which they delegate calls to their post... methods post... - woesss