-2

In an android app I have a layout, and when the user presses a button a different layout is shown (in the same activity). But instead to show the different layout right away, I want to put in a small delay (ca. 0.5 seconds). How to do that best in android?

In python you just do time.sleep(0.5) but I have seen already dozen of code lines to do the same. How to do this the most simple way as possible?

Alex
  • 41,580
  • 88
  • 260
  • 469

3 Answers3

3
public void onClick(View v) {
  v.postDelayed(new Runnable() {
    public void run() {
      // do your delayed work
    }
  }, DELAY_PERIOD);
}

where DELAY_PERIOD is the delay period in milliseconds. Here, onClick() is in your OnClickListener implementation, or is the method pointed to by android:onClick in your layout, or is pointed to by the data binding framework.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

There is Thread.sleep(timeInMs) but it is not the best option because it will suspend the main (ui thread).

It would be better to use Handler

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        someAction();
    }
}, 500) 
Rostyslav Roshak
  • 3,859
  • 2
  • 35
  • 56
  • [Be careful when creating instances of `Handler` like that](http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html). – CommonsWare Jan 18 '17 at 19:42
  • 1
    yes, of course, it is usually created as class member. Also if you submit long actions with long delay, you should consider of canceling them with `removeCallbacksAndMessages` to prevent from activity leaks. – Rostyslav Roshak Jan 18 '17 at 19:45
-1

The easiest way is to use a Handler:

import android.os.Handler;

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        // Do something
    }
}, 500);
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
  • While I think a downvote is a bit harsh (for whoever did that), [be careful when creating instances of `Handler` like that](http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html). – CommonsWare Jan 18 '17 at 19:42
  • @CommonsWare, I read the article & your answer above. I still don't understand what is the real difference between the 2 implementations. According to the documentations View.postDelayed() "Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the user interface thread." This means that your implementation (in particular, your Runnable) would also leak the activity for 10 seconds. This is all true unless the Handler behaves differently than the View. – Doron Yakovlev Golani Jan 18 '17 at 20:15
  • In this specific case, the use of `Handler` may be comparable to calling `postDelayed()` directly on the `View`. However, in general, creating instances of `Handler` requires care. In cases where there is an equivalent non-`Handler` solution (e.g., calling `postDelayed()` on the `View`), I recommend the non-`Handler` solution, just so that people do not get into the habit of just creating `Handler` instances whenever it suits them. That's one of the reasons why we wrap `Handler` objects in other things (e.g., `AsyncTask`), and it's why Lint will complain about your code. – CommonsWare Jan 18 '17 at 20:22
  • I think the only solution here is to cancel the request when the activity... otherwise the solutions are the same (see http://stackoverflow.com/questions/41728973/what-is-the-difference-between-view-postdelayed-and-handler-postdelayed-on-t). -1 for the downvote... – Doron Yakovlev Golani Jan 18 '17 at 22:08