Reputation: 3953
I am working on an Android application in which I am using voice recording functionality, for this I am making the button tapping for the recording like if user hold the button for more than 3 seconds then it will allow for the recording.
For this I am using Touch listener, but the problem is that if user just click the button without keeping it hold then the recording or function is called. My code is given below:
mBtnSendAudio.setOnTouchListener(new OnTouchListener() {
boolean shouldRecord = true;
@Override
public boolean onTouch(View v, MotionEvent event) {
if(!canChat) {
Toast.makeText(getBaseContext(), "Turned Off Chat", Toast.LENGTH_SHORT).show();
return true;
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mLastDown = System.currentTimeMillis();
shouldRecord = true;
startRecording();
mLLAudioRecordingHover.setVisibility(View.VISIBLE);
rectAudioButton = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
mStartTime = System.currentTimeMillis();
mRecordHandler.removeCallbacks(mUpdateTimeTask);
mRecordHandler.postDelayed(mUpdateTimeTask, 100);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (shouldRecord) {
mLLAudioRecordingHover.setVisibility(View.GONE);
mLastDuration = System.currentTimeMillis() - mLastDown;
mRecordHandler.removeCallbacks(mUpdateTimeTask);
mLLAudioRecordingHover.setVisibility(View.GONE);
stopRecording(true, mLastDuration);
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
// finger move outside of button's bounds
if (!rectAudioButton.contains(v.getLeft() + (int) event.getX(), v.getTop()+ (int) event.getY()) && shouldRecord) {
mRecordHandler.removeCallbacks(mUpdateTimeTask);
shouldRecord = false;
stopRecording(shouldRecord, 0);
mLLAudioRecordingHover.setVisibility(View.GONE);
}
}
return v.onTouchEvent(event);
}
});
Upvotes: 0
Views: 580
Reputation: 2757
Once try as follows
mBtnSendAudio.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
mRecordHandler.postDelayed(run, 3500/* OR the amount of time you want */);
break;
default:
mRecordHandler.removeCallbacks(run);
break;
}
return true;
}
});
and here is Runnable to change status of recording
Runnable run = new Runnable() {
@Override
public void run() {
shouldRecord = true;
}
};
UPDATE :
Then you have to use GestureDetector as follows
private GestureDetector gestureDetector = new GestureDetector(this, new SimpleOnGestureListener {
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
return true;
}
});
then in onTouch(-,-) of OnTouchListener
if (gestureDetector.onTouchEvent(arg1)) {
// single tap
//write your code for single click
return true;
}
Hope it will helps you.
Upvotes: 1
Reputation: 3785
you have two choices right here: first you can forget about the 3 seconds and use View.OnLongClickListener which is a standard listener used by most of the developers to detect a press and hold event or you can set a timer inside if (event.getAction() == MotionEvent.ACTION_DOWN) {}
condition here is the code for a 3 second timer:
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//your caode
}
}.start();
use your desired functionality inside onFinish()
method.
Upvotes: 0