Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reset the time of a postDelayed runnable in onClick?

I've got a listView that gets populated from a server. In the onClick of the ListItem, I display a button for a x number of seconds and I make it invisible again. How can I reset the time every time the onClick is called? Here is my listItem onClick:

private void displayInCallButton() {


    mButton.setEnabled(true);

    if (canDisplayInCallControlls) {
        canDisplayInCallControlls = false;

        fadeInAnimation(mButton);
        mButton.setEnabled(true);

        mFrontView.postDelayed(new Runnable() {
            public void run() {
                fadeOutAnimation(mButton);
                mButton.setEnabled(false);
                hasAnimationEnded = true;
                canDisplayInCallControlls = true;


            }
        }, 5000);

    }
}

Thank you in advance.

like image 622
user2492806 Avatar asked Nov 20 '15 13:11

user2492806


People also ask

How do I start a delayed activity?

You can use the method postDelayed(Runnable action, long delayMillis) of a View to add a Runnable to the message queue to be run after an (approximate) delay. This will allow for your desired behaviour without blocking the UI thread.

What is Post delayed?

postDelayed(Runnable r, Object token, long delayMillis) Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. final void. removeCallbacks(Runnable r) Remove any pending posts of Runnable r that are in the message queue.

What is handler and Runnable in Android?

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . Each Handler instance is associated with a single thread and that thread's message queue.

When to use handler in Android?

In android Handler is mainly used to update the main thread from background thread or other than main thread. There are two methods are in handler. Post() − it going to post message from background thread to main thread using looper.


2 Answers

You have to remove the callbacks and set it once again with the new one with the reset time.

first, set the call back like

Runnable myRunnable = new Runnable() {
   @Override
   public void run() {
    fadeOutAnimation(mButton);
    mButton.setEnabled(false);
    hasAnimationEnded = true;
    canDisplayInCallControlls = true;
   }
};

then set it to mFrontView like,

mFrontView.postDelayed(myRunnable,5000)

If you want to reset, do it like

mFrontView.removeCallbacks(myRunnable);
mFrontView.postDelayed(myRunnable, 2000);
like image 163
M.Prabha karan Avatar answered Oct 18 '22 05:10

M.Prabha karan


How can I reset the time every time the onClick is called?

There is no built-in mechanism to accomplish that.

You can, however, keep a reference to the Runnable you post, remove it and then repost it again to restart at the original delay.

The result would look somewhat like this in its most simple form:

Runnable mDelayedRunnable = new Runnable() {
    @Override public void run() {
            fadeOutAnimation(mButton);
            mButton.setEnabled(false);
            hasAnimationEnded = true;
            canDisplayInCallControlls = true;
        }
    };

private void displayInCallButton() {
    mButton.setEnabled(true);

    if (canDisplayInCallControlls) {
        canDisplayInCallControlls = false;

        fadeInAnimation(mButton);
        mButton.setEnabled(true);

        mFrontView.removeCallbacks(mDelayedRunnable);
        mFrontView.postDelayed(mDelayedRunnable, 5000);
    }
}

You can safely call removeCallbacks() with a Runnable that was never posted in the first place (or even null).

If you don't want to keep an explicit reference to the Runnable, you could also opt to tag the view with it. Just don't forget to clean up on i.e. orientation changes and the like.

like image 3
MH. Avatar answered Oct 18 '22 03:10

MH.