4

My app is built with around 50-60 screens. Each screen has a button to open a new screen. Now the issue I am facing is when user double tap button, a new screen is open twice.

For this issue, I found a solution like below.

 if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
                return;
            }

            mLastClickTime = SystemClock.elapsedRealtime();

But to prevent double click, I need to write the above code in each button click. I have not created common custom button which used everywhere.

Is there any way to double tap on app level?

Rohan Patel
  • 1,758
  • 2
  • 21
  • 38

6 Answers6

2

i got same issue i solved it as below it might be helpfull for you.

you can achive by two ways One: try to using a boolean variable:

public class Blocker {
    private static final int BLOCK_TIME = 1000;
    private boolean isBlockClick;

    /**
     * Block any event occurs in 1000 millisecond to prevent spam action
     * @return false if not in block state, otherwise return true.
     */
    public boolean block(int blockInMillis) {
        if (!isBlockClick) {
            isBlockClick= true;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    isBlockClick= false;
                }
            }, blockInMillis);
            return false;
        }
        return true;
    }

    public boolean block() {
        return block(BLOCK_TIME );
    }
} 

use this as below in every click.

button.setOnClickListener(new View.OnClickListener() {
            private Blocker blocker = new Blocker();

            @Override
            public void onClick(View v) {
                if (!blocker.block(block-Time-In-Millis)) {
                    // do your action   
                }
            }
        });

Two or you can set button.setEnable(false) on every clickevent of button as below

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                btn.setEnabled(false);
            }
        });
YuvrajsinhJadeja
  • 1,383
  • 7
  • 23
1

actually you can set the activities not to have multiple instances by adding the flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT to the intent.

see answer from other question

If the activity is in the stack, it will not be created twice

Yoav Gibri
  • 158
  • 5
0

use this custom class it can handle any doubletab or single tab on button click event

public class DoubleTabCustomButton implements View.OnClickListener {

    private boolean isRunning = true;
    private int resetInTime = 500;
    private int counter = 0;

    private DoubleClickCallback listener;

    public DoubleTabCustomButton(Context context) {
        listener = (DoubleClickCallback) context;
    }

    @Override
    public void onClick(View v) {

        if (isRunning) {
            if (counter == 1) {
                listener.onDoubleClick(v);
            }
            else if (counter==0){
                listener.onSingleClick(v);
            }

            counter++;

            if (!isRunning) {
                isRunning = true;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(resetInTime);
                            isRunning = false;
                            counter = 0;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }

        }

    }
}

it's interface

public interface DoubleClickCallback {

    public void onDoubleClick(View v);

    public void onSingleClick(View V);
}

and finally you can use in activity like this

public class ButtonDoubleTab extends AppCompatActivity implements DoubleClickCallback {

    Button btndoubletab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_double_tab);

        btndoubletab=findViewById(R.id.btndoubletab);
       // btndoubletab.setOnClickListener(this);
        btndoubletab.setOnClickListener(new DoubleTabCustomButton(this));
    }
    @Override
    public void onDoubleClick(View v) {
        //do double tab action
    }

    @Override
    public void onSingleClick(View V) {
       //single tab action 
    }
}
Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
Black mamba
  • 462
  • 4
  • 15
0

If you have a base activity class, you can override the startActivity(Intent) method to add the Intent.FLAG_ACTIVITY_REORDER_TO_FRONT


abstract class BaseActivity: AppCompatActivity() {

    final override fun startActivity(intent: Intent) {
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
        super.startActivity(intent)
    }
}
ArmandoNCM
  • 26
  • 3
-1

Use this inline function:

 inline fun View.onSingleClick(minimumClickInterval: Long = 800, crossinline onClick: (View?) -> Unit) {
        var isViewClicked = false
        var mLastClickTime = 0L

        setOnClickListener { view ->

            val currentClickTime = SystemClock.uptimeMillis()
            val elapsedTime = currentClickTime - mLastClickTime

            mLastClickTime = currentClickTime

            if (elapsedTime <= minimumClickInterval)
                return@setOnClickListener

            if (!isViewClicked) {
                isViewClicked = true

                Handler(Looper.getMainLooper()).postDelayed({ isViewClicked = false }, 600)
            } else {
                return@setOnClickListener
            }

            onClick(view)

            Log.d(this.javaClass.simpleName, "onSingleClick successfully called")
        }
    }

Use with any view like this:

button.onSingleClick {
    // do something here on the button click  
}

You can also set the minimum click interval like this:

button.onSingleClick(1000) {
    // do something here on the button click  
}
MojoJojo
  • 783
  • 5
  • 15
-3

make the button disable on click .

b.setEnabled(false);

You can make it back enable it onResume or any other certain callback

  b.setEnabled(true);
Soham
  • 4,397
  • 11
  • 43
  • 71
  • 1
    Think how it would be a terrible user experience to have a button suddenly disabled once it has just been clicked. – Léa Gris Sep 11 '19 at 08:04
  • I am really surprised that you haven't seen any app where you click the button and it disabled the button for a while with a message or color.@LéaGris – Soham Sep 11 '19 at 08:23
  • For example have you seen any app where it is asking for OTP ? Can you click it multiple times?@LéaGris – Soham Sep 11 '19 at 08:25