Search code examples
androidontouchlistener

Android code to apply same double tap touch listener on two or more different custom views


How to apply same double tap touch listener on two or more different custom views?

view1.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }
});
view2.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }
});
mGestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {
    @Override
    public void onLongPress(MotionEvent e) 
    {
        displayPTZControlsPopup(corresponding view id); 
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) 
    {
        Intent intent = new Intent(VideoView.this,FullScreenVideo.class);
        startActivity(intent);
        return true;
    }

    @Override
    public boolean onDown(MotionEvent e) 
    {
        return true;
    }
});
mGestureDetector.setIsLongpressEnabled(true);

can anyone help me?


Solution

  • Do like this :

    view1.setOnTouchListener(this);
    view2.setOnTouchListener(this);
    
    @Override
    public boolean onTouch(final View view, MotionEvent event) {
        if(view.getId() == idOfYourView1 || view.getId() == idOfYourView2) {
            //Apply the method to one of the view touched
        }
    }