Ncit Cosmos Ncit Cosmos - 23 days ago 7
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

In your activity before onCreate declare

private long lastclicked = 0;

then setOnClickListener

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


    //perform here your task

   }
}