How to set delay in Android onClick function How to set delay in Android onClick function android android

How to set delay in Android onClick function


Instead of sleeping in the onclick, you could post a delayed message to a handler (with associated runnable) and have it update the UI. Obviously fit this into the design of your app and make it work, but the basic idea is this:

//Here's a runnable/handler comboprivate Runnable mMyRunnable = new Runnable(){    @Override    public void run()    {       //Change state here    } };

Then from onClick you post a delayed message to a handler, specifying that runnable to be executed.

Handler myHandler = new Handler();myHandler.postDelayed(mMyRunnable, 1000);//Message will be delivered in 1 second.

Depending on how complicated your game is, this might not be exactly what you want, but it should give you a start.


Correctly work:

final Handler handler = new Handler();handler.postDelayed(new Runnable() {  @Override  public void run() {    //Do something after 100ms  }}, 100);


You never should sleep in UI thread (by default all your code runs in UI thread) - it will only make UI freeze, not let something change or finish. I can't suggest more because I don't understand code logic.