Reputation: 111
I have a button that when click shows a dialog. But when you click the button quickly in multiple times, it will show 2 or more dialog on the screen. Depends on how many times you click the button before dialog shows. So I have to close each dialog many times...
I already used dialog.isShowing but it seems that it will ignore it when you click the button quickly in multiple times.
...So I want to click button at a time when dialog is closed.
private var mFlag = false
fun myButton(view : View) {
var tempDialog = AlertDialog.Builder(this).create()
if (!mFlag) {
myDialog.show()
mFlag = true
}
if(dialog.isShowing){
mFlag = false
}
}
Upvotes: 0
Views: 7052
Reputation: 796
Kotlin(with adjustable delay):
class OnSingleClickListener(private val delay: Long) : View.OnClickListener {
private var lastClickTime = 0L
override fun onClick(view: View) {
val currentTime = System.currentTimeMillis()
if (currentTime - lastClickTime < delay) { return }
lastClickTime = currentTime
}
}
fun View.setOnSingleClickListener(delay: Long = 500L) {
setOnClickListener(OnSingleClickListener(delay))
}
from here
Upvotes: 0
Reputation: 1546
I have made public method for avoid double clicking issue on view.
Please check this method,
/***
* To prevent from double clicking the row item and so prevents overlapping fragment.
* **/
public static void avoidDoubleClicks(final View view) {
final long DELAY_IN_MS = 900;
if (!view.isClickable()) {
return;
}
view.setClickable(false);
view.postDelayed(new Runnable() {
@Override
public void run() {
view.setClickable(true);
}
}, DELAY_IN_MS);
}
You can use the method by following way,
buttonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(String clickedText) {
Utils.avoidDoubleClicks(alertViewHolder.tv_alert);
// Rest code here for onlick listener
});
private long lastClickTime = 0;
View.OnClickListener buttonHandler = new View.OnClickListener() {
public void onClick(View v) {
// preventing double, using threshold of 1000 ms
if (SystemClock.elapsedRealtime() - lastClickTime < 1000){
return;
}
lastClickTime = SystemClock.elapsedRealtime();
}
}
Upvotes: 14