Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Generally the right way to detect a long-click is by implementing <a href="http://developer.android.com/reference/android/view/View.OnLongClickListener.html" rel="nofollow">View.OnLongClickListener</a>. This is easier and less error-prone that detecting it yourself, and it ensures that your app fits in well with the rest of the system.</p> <p>For a custom <code>View</code>, you would add <code>implements View.OnLongClickListener</code> to your class declaration, add <code>setOnLongClickListener(this);</code> to the constructor, and add the <code>onLongClick()</code> method to your class:</p> <pre><code>public boolean onLongClick (View v) { // Handle long-click } </code></pre> <p>If your <code>View</code> isn't custom you can add the listener like this:</p> <pre><code>theView.setLongClickable(true); theView.setOnLongClickListener(new View.OnLongClickListener() { public boolean onLongClick(View v) { // Handle long-click } }); </code></pre> <p><strong>Update:</strong> Okay, I've done some digging, and while this is the right way to do it for every View I've ever worked with, it in fact does not work for SeekBars, which is what the questioner is working with. This fact is not documented in the <code>SeekBar</code> documentation, but experimentation has shown it to be true, and digging through the source code shows why: <code>SeekBar.onTouchEvent()</code> does not call <code>super.onTouchEvent()</code>. It is in <code>View.onTouchEvent()</code> that <code>performLongClick()</code> is called, if appropriate.</p> <p>The way I would implement it if I had to is with <code>Handler.postDelayed()</code>. On ACTION_DOWN, I would post (with e.g. 3000ms delay) a <code>Runnable</code> that handles the long-press to a <code>Handler</code>, and I'd cancel it on ACTION_UP. So any press for less than the delay doesn't end up calling the <code>Runnable</code>, but if ACTION_UP hasn't occurred after the delay, it is.</p> <p>That said, I'd caution you to rethink doing this at all. What does it mean to long-press a <code>SeekBar</code>? If the user is dragging the "thumb" for longer than the 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload