How to swipe a button on touch event android with directions left,right, up,down..?...I am new to android and want to develop a callback application.Please help by providing some links
callbackButton.setOnTouchListener(new OnTouchListener() {
private float currX;
private float currY;
@Override
public boolean onTouch(View v, MotionEvent event) {
float x1 = 0, x2, y1 = 0, y2, dx, dy , oldx =0,oldy=0;
String direction;
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
oldx = event.getX();
oldy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
x2 = event.getX();
y2 = event.getY();
dx = x2-x1;
dy = y2-y1;
// Use dx and dy to determine the direction
if(Math.abs(dx) > Math.abs(dy)) {
if(dx>0) {
direction = "right";
Log.e("right...","moving..");
}else{
direction = "left";
Log.e("left...","moving..");
}
} else {
if(dy>0) {
direction = "down";
Log.e("down...","moving..");
currX = event.getRawX();
currY = event.getRawY();
Log.e("x=", ""+(currX-oldx));
Log.e("y=", ""+(currY-oldy));
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
marginParams.setMargins((int)(currX-oldx), (int)(currY-oldy),0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
v.setLayoutParams(layoutParams);
} else {
direction = "up";
Log.e("up...","moving..");
}
}
}
return true;
}
});
This is my code for imageButton moving on touch event. But this code is not working as I want
Check my blogspot in which I have shown the drag drop image
onTouch
event.Edit
: here is the full code:First of all create android project in your eclipse and create a class and layout file as below:
MainActivity.java
main_layout.xml
Here is the boundary file xml code which shows the border of the layout in which object moves.
Here you will find an explaination on how to handle touch events:
http://developer.android.com/guide/topics/ui/ui-events.html
Then, in the on touch method, you need to
Example (using code from http://developer.android.com/training/graphics/opengl/touch.html)
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)
Good luck with your app
EDIT:
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.
You should try to find if a library that detects swipe movements already exists. I'm pretty sure there is many out there
EDIT 2: 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
onDraw(Graphics g)
. 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