Reputation: 137
My scenario is like this.
Start the audio recording if user pressed the record button for more than 1 seconds otherwise consider it as a accidental touch and do not record.
We have tried a solution based on long click and touch listner and the code is below. But we cannot get the toast messages in proper order. the long click listener is invoked even if it is not long click.
imgViewMic.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getActivity(), "Long Click", Toast.LENGTH_LONG).show();
imgViewMic.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Toast.makeText(getActivity(), "ACTION_DOWN", Toast.LENGTH_LONG).show();
} else if (event.getAction() == MotionEvent.ACTION_UP){
Toast.makeText(getActivity(), "ACTION_UP", Toast.LENGTH_LONG).show();
}
return true;
}
});
return true;
}
});
Upvotes: 0
Views: 1278
Reputation: 45062
Try this
//Global varibles
double startTime, deltaTime;
//Touch listener
findViewById(R.id.mic).setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Touch start time
startTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_UP:
// TouchEnd Time
deltaTime = (System.currentTimeMillis() - startTime);
// Difference > 1 Sec
if (deltaTime > 1000) {
Toast.makeText(getApplicationContext(), "Record " + deltaTime / 1000 + " Sec", 300).show();
} else {
Toast.makeText(getApplicationContext(), "Dont Record " + deltaTime / 1000 + " Sec", 300).show();
}
break;
default:
break;
}
return true;
}
});
Upvotes: 1