1

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.

Aleem Ahmed
  • 189
  • 1
  • 1
  • 11

1 Answers1

2

Under ACTION_UP in your onTouchListener, you have to manually call view.performClick(). This will trigger your onclick listener, which you can then use to open your web page.

So something like this:

case MotionEvent.ACTION_UP:
    view.performClick();
    break;

and then

    onButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do stuff
        }
    });

EDIT

Hmm. So apparently doing that will drown out the drag and drop aspect of it. Try this instead (from here and modified somewhat for your class).

In your ontouch class:

//create a gesturedetector
private GestureDetector gestureDetector = new GestureDetector(this, new SingleTapConfirm());

@Override
public boolean onTouch(View view, MotionEvent event) {
//...etc
switch (action ) {
    if (gestureDetector.onTouchEvent(view)) {
                // this is a "click", put your click event here
                return true;
    } else {
           //your original touch drag and drop code should go here
           case MotionEvent.ACTION_DOWN: {
           //...
           }

           case MotionEvent.ACTION_MOVE:
           {
            //...
           break;
           }
           //etc
    }

}

    return false;
}


private class SingleTapConfirm extends SimpleOnGestureListener {

    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
        return true;
    }
}

}

With this, you shouldn't need an onclick listener anymore. See if that works.

Community
  • 1
  • 1
Matter Cat
  • 1,538
  • 1
  • 14
  • 23
  • Showing an error on onButton.setOnClickListener and also cant even start another activity it says startactivity not defined for ontouchlistener – Aleem Ahmed Mar 18 '15 at 09:18
  • Where are you defining your onclick listener? It should be in your main activity, not the ontouchlistener. – Matter Cat Mar 18 '15 at 09:22
  • as i mentioned i have made a different class. – Aleem Ahmed Mar 18 '15 at 09:36
  • `and i call it in my main activity by this` <-- from your post. So I presume you're setting your ontouchlistener in your MainActivity. Just stick the onclick listener under it. – Matter Cat Mar 18 '15 at 09:38
  • Did the same at first but it doesnt really work, it takes me to the website but the button is no more dragable than – Aleem Ahmed Mar 18 '15 at 09:50