Ncit Cosmos Ncit Cosmos - 1 year ago 77
Android Question

Twice press button for functionality on android

I want to implement the button on android. That button has two functions, single press to toast message, if the user instantly press button twice within millisecond then it will show the alert dialog, if delay to press than it will show toast message.
Please give me some idea how to perform these functions android Button.

Answer Source

In your activity before onCreate declare

private long lastclicked = 0;

then setOnClickListener

 button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
     //1000 replace your time in ms
    if (SystemClock.elapsedRealtime() - lastclicked < 1000){
    lastclicked = SystemClock.elapsedRealtime();

    //perform here your task

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download