1

I m doing counting on how long the button was pressed right after ACTION_DOWN, depending on how long it was pressed - I respond to the press differently in ACTION_UP.

The problem is that when i do action_down(press), it does not count down to the end, and sometime it does.

Code:

    @Override
    public boolean onTouch(final View v, final MotionEvent e) {

        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                still_down = false;

                main_timer = new CountDownTimer(500, 4) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        Log.wtf("x", "timer: " + millisUntilFinished);
                        if ((int) millisUntilFinished < 100 && e.getAction() == MotionEvent.ACTION_DOWN) {
                    // above is where i detect that timer does not count down to 0, it stops at 119,107 etc
                            still_down = true;
                        }
                    }
                    @Override
                    public void onFinish() {
                        if (still_down) {
                            Log.wtf("x", "SHOW THE LARGE CARD!!!!!!!!!!!!!!!!!");
                            answer.setTextSize(50);
                            RelativeLayout.LayoutParams x = new RelativeLayout.LayoutParams(350, 500);
                            x.setMargins(margin_long_press_x1, margin_long_press_y1, 0, 0);
                            answer.setLayoutParams(x);
                            answer.requestLayout();
                        }
                    }
                };
                main_timer.start();

                break;
            case MotionEvent.ACTION_UP:
                if (still_down) {

                    //image becomes small size again
                    main_timer.cancel();
                } else {
                    action_on_single_tap( e);
                }
                break;
        }

        return true;
    }

I put a log in onTick()and see that the timer does not always count to 0.

Are there alternatives to counting how long button is pressed in android? I dont know any other ways.

ERJAN
  • 23,696
  • 23
  • 72
  • 146

2 Answers2

1

You were on the right track, have a look at this question its almost identical.

I found this most helpful

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction()==MotionEvent.ACTION_DOWN) {
        this.start = System.currentTimeMillis();
    } else if (event.getaction()==MotionEvent.ACTION_UP) {
        this.end = System.currentTimeMillis();
    }
}
Community
  • 1
  • 1
Richard Olthuis
  • 217
  • 2
  • 10
1

In onTick, the action interpreted by the if statement is always ACTION_DOWN because that is the case where you define it. That's why it always stop right around the 100 threshold you have set.

If you move the countdown timer declaration out of the onTouch handler, it makes it easier to see how they are separated. Try this.

private static final long TIMER_DURATION = 500;

private CountDownTimer countDownTimer = new CountDownTimer(TIMER_DURATION, 4) {
    @Override
    public void onTick(long millisUntilFinished) {
        // Don't need to do anything here
    }

    @Override
    public void onFinish() {
        Log.i("TimedTouchActivity", "Countdown timer finished!");
        // answer.setTextSize(50);
    }
};

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN) {
        Log.i("TimedTouchActivity", "Touch down");
        countDownTimer.start();
    } else if (action == MotionEvent.ACTION_UP) {
        Log.i("TimedTouchActivity", "Touch up");
        long eventDuration = event.getEventTime() - event.getDownTime();
        if (eventDuration < TIMER_DURATION) {
            Log.i("TimedTouchActivity", "Short");
            countDownTimer.cancel();
            // single click
        } else {
            Log.i("TimedTouchActivity", "Long");
            // answer.setTextSize(20);
        }
    }
    return true;
}
mpkuth
  • 6,994
  • 2
  • 27
  • 44
  • great that you spotted flaw in my logic.. thx , i will try this code and see if it works – ERJAN Oct 12 '15 at 03:20