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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With