Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here you will find an explaination on how to handle touch events:</p> <p><a href="http://developer.android.com/guide/topics/ui/ui-events.html" rel="nofollow">http://developer.android.com/guide/topics/ui/ui-events.html</a></p> <p>Then, in the on touch method, you need to </p> <ul> <li>detect when the user put his finger down</li> <li>store the finger's position</li> <li>detect when the user moves</li> <li>compare with previous position</li> <li>detect when the user stops touching the screen</li> </ul> <p>Example (using code from <a href="http://developer.android.com/training/graphics/opengl/touch.html" rel="nofollow">http://developer.android.com/training/graphics/opengl/touch.html</a>)</p> <pre><code>@Override public boolean onTouchEvent(MotionEvent e) { // MotionEvent reports input details from the touch screen // and other input controls. In this case, you are only // interested in events where the touch position changed. float x = e.getX(); float y = e.getY(); switch (e.getAction()) { case MotionEvent.ACTION_DOWN: mIsDown = true; break; case MotionEvent.ACTION_MOVE: float dx = x - mPreviousX; float dy = y - mPreviousY; // Here you can try to detect the swipe. It will be necessary to // store more than the previous value to check that the user move constantly in the same direction detectSwipe(dx, dy); case MotionEvent.ACTION_UP: mIsDown = false; break; } mPreviousX = x; mPreviousY = y; return true; } </code></pre> <p>With this simple case, you wouldn't even need to stor when the user puts his finger down or up (since moving implies a finger down) but it can be useful to have the boolean value stored)</p> <p>Good luck with your app</p> <p><strong>EDIT:</strong></p> <p>It seems you edited your post with some code. You say that you don't get the explected results but you don't say what you get. This might be useful for us to help you.</p> <p>You should try to find if a library that detects swipe movements already exists. I'm pretty sure there is many out there</p> <p><strong>EDIT 2:</strong> I assume you button is a simple android.Button. One solution could be to create a class that extends Button (ex: MySwipableButton). in your xml, you create a layout that contains your MySwipableButton, and give it enought place to be moved (for example, it has width=fill_parent, since you want it to swipe on the while screen). MySwipableButton implements onTouch to store the position in which the button should be (using the method you already have) MySwipableButton would also overwrite <code>onDraw(Graphics g)</code>. In onDraw, you would paint the button (super.draw()) at the place it must be (regarding the current swipe) and leave the rest of the view empty</p>
 

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