In an Android app, I have a feature to record audio. The idea is to have a button that has 2 types of actions.
I can click on button and start recording, and when I click again it stops recording.
I can hold the button and while its being held the app records, when I release it, the app stops recording.
I tried with a OnTouchListener
private static int CLICK_ACTION_THRESHHOLD = 250;
public long lastTouchDown;
public boolean isClick;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()){
case R.id.main_record_button:
case R.id.main_record2:
if (event.getAction() == MotionEvent.ACTION_DOWN) {
lastTouchDown = System.currentTimeMillis();
//... do stuff
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
if (System.currentTimeMillis() - lastTouchDown < CLICK_ACTION_THRESHHOLD) {
isClick = true;
//...do other stuff
}
Whats the best way to achieve this ?