Mercy Angel
Mercy Angel

Reputation: 81

How to get onTouch x and y value in setOnLongClickListener?

Run Time I have change the background image(set background). In the image, I have perform the action using Touch Listener. But in my case, I need another action on Touch event. For that purpose I need to proceed with long touch event. Suggest me, any other idea.

Upvotes: 2

Views: 769

Answers (2)

Amrut Bidri
Amrut Bidri

Reputation: 6360

Answering from here:

final Handler handler = new Handler(); 
Runnable mLongPressed = new Runnable() { 
    public void run() { 
        Log.i("", "Long press!");
        if(myEvent!=null)
        {
           int requiredXvalue=myEvent.getX();               
           int requiredYvalue=myEvent.getY();

        }
    }   
};
MotionEvent myEvent;
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView){
    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        handler.postDelayed(mLongPressed, 1000);
        myEvent=event;
    }
    if((event.getAction() == MotionEvent.ACTION_MOVE)||(event.getAction() == MotionEvent.ACTION_UP))
        handler.removeCallbacks(mLongPressed);
    return super.onTouchEvent(event, mapView);
}

OR

you can use Gesture Detector as answered here:

final GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
    public void onLongPress(MotionEvent e) {
        Log.e("", "Longpress detected");
        int requiredXvalue=e.getX();               
        int requiredYvalue=e.getY();
    }
});

public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
};

Upvotes: 2

Don Chakkappan
Don Chakkappan

Reputation: 7560

         imageButton.setOnLongClickListener(new OnLongClickListener() { 

                    @Override
                    public boolean onLongClick(View v) {
                        // TODO Auto-generated method stub
                        return false;
                    }
                });

Upvotes: 0

Related Questions