Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When i was about to post my answer i found out some one already got some kind of solution....</p> <p>But here is mine, simple and works like a charm. Just one flag ;)</p> <p>This code detects shortpresses and longpresses, when a longpress occurs no shortpress will be fired!</p> <p><strong>Note:</strong> if you want the normal volume up and down behavior change the return true in the onKeyPress method to the super call like this:</p> <pre><code>event.startTracking(); if(event.getRepeatCount() == 0){ shortPress = true; } //return true; return super.onKeyDown(keyCode, event); </code></pre> <p>Code without the super call:</p> <pre><code>private boolean shortPress = false; @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { shortPress = false; Toast.makeText(this, "longPress", Toast.LENGTH_LONG).show(); return true; } //Just return false because the super call does always the same (returning false) return false; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { if(event.getAction() == KeyEvent.ACTION_DOWN){ event.startTracking(); if(event.getRepeatCount() == 0){ shortPress = true; } return true; } } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { if(shortPress){ Toast.makeText(this, "shortPress", Toast.LENGTH_LONG).show(); } else { //Don't handle longpress here, because the user will have to get his finger back up first } shortPress = false; return true; } return super.onKeyUp(keyCode, event); } </code></pre> <p><strong>Code down here is with the volume up key added, just pick your side ;)</strong></p> <pre><code>private boolean shortPress = false; @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { shortPress = false; Toast.makeText(this, "longPress Volume Down", Toast.LENGTH_LONG).show(); return true; } else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){ shortPress = false; Toast.makeText(this, "longPress Volume Up", Toast.LENGTH_LONG).show(); return true; } //Just return false because the super call does always the same (returning false) return false; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) { if(event.getAction() == KeyEvent.ACTION_DOWN){ event.startTracking(); if(event.getRepeatCount() == 0){ shortPress = true; } return true; } } return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { if(shortPress){ Toast.makeText(this, "shortPress Volume Down", Toast.LENGTH_LONG).show(); } else { //Don't handle longpress here, because the user will have to get his finger back up first } shortPress = false; return true; } else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){ if(shortPress){ Toast.makeText(this, "shortPress Volume up", Toast.LENGTH_LONG).show(); } else { //Don't handle longpress here, because the user will have to get his finger back up first } shortPress = false; return true; } return super.onKeyUp(keyCode, event); } </code></pre>
    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. 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.
    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