Skip to content

Instantly share code, notes, and snippets.

@rocboronat
Last active November 29, 2020 12:42
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rocboronat/bf1e7e43c0fd11907867 to your computer and use it in GitHub Desktop.
Save rocboronat/bf1e7e43c0fd11907867 to your computer and use it in GitHub Desktop.
A simple way to call the common new Handler().postDelayed(..., time);
package com.fewlaps.android.quitnow.base.customview;
import android.os.Handler;
import android.view.View;
/**A simple way to call the common new Handler().postDelayed(..., time);
*
* Created by Roc Boronat on 12/12/2014.
*/
public class RippleDelayedRunner implements View.OnClickListener {
public static final long DEFAULT_DELAY = 300;
public static void runDelayed(Runnable runnable, long delay) {
new Handler().postDelayed(runnable, delay);
}
public static void runDelayed(long delay, Runnable runnable) {
new Handler().postDelayed(runnable, delay);
}
public static void runDelayed(Runnable runnable) {
new Handler().postDelayed(runnable, DEFAULT_DELAY);
}
Runnable runnable;
long delay = DEFAULT_DELAY;
public RippleDelayedRunner(Runnable runnable) {
this.runnable = runnable;
}
public RippleDelayedRunner(Runnable runnable, long delay) {
this.runnable = runnable;
this.delay = delay;
}
@Override
public void onClick(View view) {
runDelayed(runnable, delay);
}
}
@Aracem
Copy link

Aracem commented Feb 2, 2015

Good one!

I suggest a few changes:

First one of the runDelayed method is not needed because is the same but with the arguments in different order.
Check in the onClick method if the user is in at least Lollipoop version and then launch with delay. If not, launch without delay.

@rocboronat
Copy link
Author

Great! I've seen you fork, it's a good approach!

In our case, we need the delay in all versions of Android, 'cause we're playing the ripple effect in all Android versions. But I've seen the check about the L version in your fork, nice work!

About two methods: you also hate to write the whole Runnable and add something like "}, 250);" at the end, don't you? That's the reason why we created two ways to do the same: the common legacy way, and an alternative way to care my eyes and ease readability :·)

@pocketutilities
Copy link

'Handler()' is deprecated now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment