0

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
        }
    }
Kevin Robatel
  • 7,570
  • 3
  • 41
  • 56
xigncode23
  • 101
  • 2
  • 9

1 Answers1

14

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 

});

Or another way is,

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();
    }
}
Sergio Tulentsev
  • 219,187
  • 42
  • 361
  • 354
Fenil Patel
  • 1,396
  • 11
  • 27