0

I saw a similar question posted here: Detect touch event on a view when dragged over from other view. But that question has different behaviour compared to what I want.

If I have multiple views, I press on one and continue dragging my finger onto multiple other views, is there a way for the other views to be notified that they have been touched? It won't be just 2 views, it could be multiple views. I click on one and keep dragging my finger and go over multiple other views.

The views are dynamically made and programmatically added to a FrameLayout and positioned programmatically by adding margins around it.

Community
  • 1
  • 1
ashimashi
  • 463
  • 1
  • 5
  • 14

1 Answers1

0

I couldn't find a way to do this through the actual custom views and had to go one layer back.

The position of the views are documented on the FrameLayout that contains each view.

In the FrameLayout, I ended up overwriting onTouchEvent.

I ended up having to do something similar to this:

@Override
public boolean onTouchEvent(MotionEvent event) {

    float xCoord = event.getX();
    float yCoord = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Using X and Y coordinates, find out if touch event is on top of a view.
            // Mark the view as touched
            return true;
        case MotionEvent.ACTION_UP:
            // Action required once touch released.
            return true;
        default:
           // When dragging, the default case will be called.
           // Using X and Y coordinates, figure out if finger goes over a view.
           // If you don't want same behaviour when going back to previous view,
           // then mark it so you can ignore it if user goes back to previous view.

    }
}

Once case MotionEvent.ACTION_UP gets called, you can use the fact that the views were marked to know which ones were touched. You can also get the order if required by putting the views in an array when marking them.

ashimashi
  • 463
  • 1
  • 5
  • 14
  • 1
    So, what is the solution for detection collision? This website is not for personal draft area, it is about helping others with concrete solutions. – Talha Safdar Dec 12 '22 at 17:28