0
votes

I have an ImageView which already has an event listener which causes the image to zoom.

However, I also have a zoom button, and rather than re-implement my zooming method, I'd like to just programmatically dispatch a double-tap event to the imageview. However, I can't seem to find any useful resources for crafting and dispatching events.

How can I dispatch events programmatically to views?

2

2 Answers

0
votes

You can create your own event and then call your Activity onTouchEvent...

MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, pressure, size, metaState, xPrecision, yPrecision, deviceId, edgeFlags);
onTouchEvent(event);

I'm not sure this will work bcs it's seams to be that can be a security failure since you can force the user to "click" in some add or something like that...

0
votes

Late but was dealing with the same so I thought of answering for any other person

OK some code:

final GestureDetector gd = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {

            Animation animUpDown;

            // load the animation

            Toast.makeText(getApplicationContext(), "Item added to favorites", Toast.LENGTH_LONG).show();
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);

        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return true;
        }
    });

    viewToBeTapped.setOnTouchListener((v, event) -> gd.onTouchEvent(event));

This is all you need to do. Hope it helps someone