I wrote all the code for a button which is move-able or drag-able with a finger press, now i also want that button to perform something when it is clicked (for example go to a website when clicked in my case) I implemented all the code in a separate class like this:
public class MultiTouchListener implements OnTouchListener
{
private float mPrevX;
private float mPrevY;
public MainActivity mainActivity;
public MultiTouchListener(MainActivity mainActivity1) {
mainActivity = mainActivity1;
}
@Override
public boolean onTouch(View view, MotionEvent event) {
float currX,currY;
int action = event.getAction();
switch (action ) {
case MotionEvent.ACTION_DOWN: {
mPrevX = event.getX();
mPrevY = event.getY();
break;
}
case MotionEvent.ACTION_MOVE:
{
currX = event.getRawX();
currY = event.getRawY();
MarginLayoutParams marginParams = new MarginLayoutParams(view.getLayoutParams());
marginParams.setMargins((int)(currX - mPrevX), (int)(currY - mPrevY),0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
view.setLayoutParams(layoutParams);
break;
}
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
and i call it in my main activity by this
MultiTouchListener touchListener=new MultiTouchListener(this);
onButton.setOnTouchListener(touchListener);
Moveable drag and drop button is working fine, i just want this button to Open a website link when pressed, What changes do i have to make in my code to perform this.Thankyou.