0

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 ?

James Wood
  • 17,286
  • 4
  • 46
  • 89
Thought
  • 5,326
  • 7
  • 33
  • 69

2 Answers2

0

OnTouchListener is not very intuitive to work with.

I have used GestureDetector to track userbehavior. I think that is what you should use. Check out the implementation and callbacks here: https://developer.android.com/training/gestures/detector.html

miqueloi
  • 688
  • 4
  • 13
0

You should go through this library to understand whats he is doing or you can use it too.

enter image description here

Also the this Thread.

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41