Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register LongClick listener on a Seekbar

Tags:

android

In android, is it possible for me to register a long click listener on a seekbar?

I have done this:

mySeekBar.setLongClickable(true);
mySeekBar.setOnLongClickListener(new OnLongClickListener() {
   public boolean onLongClick (View v) {
    Log.d("TEST", "Get a long click event!!!!!!!!!!!!");
 }
});

But I don't see my debug print statement at all. Any idea about how can I achieve it?

Thank you.

like image 624
michael Avatar asked Oct 23 '22 22:10

michael


2 Answers

While not mentioned in the documentation, I've experimentally determined that both click and long-click listeners are unsupported on SeekBars. They are supported in ProgressBars.

Digging through the source code shows why: SeekBar.onTouchEvent() does not call super.onTouchEvent(). It is in View.onTouchEvent() that performClick() and performLongClick() are called, as appropriate. So the only way to handle long-clicks on a SeekBar is to manually detect them.

My own thinking is that having a long-click handler for a SeekBar doesn't actually make sense. If the user is dragging the "thumb" for longer than the long-click threshold/delay, suddenly you have a long-press that probably was not meant as one. You can cancel and reset the delay every time the thumb is moved, requiring a long-press to be several seconds in exactly one position. But it's rare to be perfectly still; it's in fact difficult to hold the thumb yet not move it for several seconds. So you could then have a minimum change in thumb position that resets the delay. That's what I would do if I had to, but I must say it's a very strange user experience. There are many places in Android where a user expects a long-press to be significant, but a SeekBar isn't one of them.

like image 87
Darshan Rivka Whittle Avatar answered Oct 27 '22 09:10

Darshan Rivka Whittle


AFAICT this is not possible to do with the OnLongClickListener. The documentation doesn't say it won't work, but I've never seen any example of this (and I never got it to work myself either).

A possible workaround (depending on what you want to achieve) could be to use the OnSeekBarChangeListener handling the long click through onStartTrackingTouch / onStopTrackingTouch callbacks.

like image 32
johlo Avatar answered Oct 27 '22 10:10

johlo