I am building an app with a panic alert button. I want to display a toast message after I hold the button for 3 seconds so if the person hits it by mistake it wont send but I am not sure how to do it.
public long startTime = 0;
Button btn = (Button)findViewById(R.id.btnAlert);
btn.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_UP){
if((System.currentTimeMillis()- startTime)> 3000){
Toast.makeText(getApplicationContext(), "Alert Received! Emergency Services Will Arrive At Your Location Shortly", Toast.LENGTH_LONG).show();
}
return true;
}
if(event.getAction() == MotionEvent.ACTION_DOWN){
startTime = System.currentTimeMillis();
return true;
}
return false;
}
});