Wait for 5 seconds

100,287

Solution 1

just add one-liner with lambda

(new Handler()).postDelayed(this::yourMethod, 5000);

edit for clarification: yourMethod refers to the method which you want to execute after 5000 milliseconds.

Solution 2

See if this works for you. Be sure to import the android.os.Handler

      Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    // yourMethod();
                }
            }, 5000);   //5 seconds

Solution 3

For import use : import android.os.Handler;

 new Handler().postDelayed(new Runnable() {
            public void run() {
                // yourMethod();
            }
        }, 5000);   //5 seconds

Solution 4

I use the following code (the same as you see before) for my android app, i need to wait some threads before start my new method. It works fine.

 Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            public void run() {
                // yourMethod();
            }
        }, 5000);   //5 seconds

Solution 5

This works for me:

    val handler = Handler()
    handler.postDelayed({
        // your code to run after 2 second
    }, 2000)
Share:
100,287
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to wait 5 seconds before starting another public void method. The thread sleep was not working for me. If there is a way of wait() without using Threads I would love to know that.

    public void check(){
        //activity of changing background color of relative layout
    }
    

    I want to wait 3 seconds before changing the relative layout color.