user1277224
user1277224

Reputation: 11

How I can I detect all ImageViews that have been touched in Android?

I am trying to make a "Dots" clone and am having difficulty trying to figure out how to detect all imageviews that have been touched with one swipe of the finger. I believe I should be using a viewgroup but not sure where to go from there, any other help would be appreciated.

Upvotes: 1

Views: 70

Answers (1)

JustSoAmazing
JustSoAmazing

Reputation: 747

There are a number of ways you could do this - here's one of them.

Create a RelativeLayout and give it an id. In your activity class, find this RelativeLayout and set an OnTouchListener on it.

Now populate a 2d array that contains your image views and add them to your RelativeLayout.

In the OnTouch callback method, you'll have access to a MotionEvent object that you can query to access motion events that describe the user's finger location as it moves across the screen. Compare the provided x and y coordinates to the x and y locations of the image views in your 2d array and if the coordinates match those of an image view, then add the selected image view to an ArrayList.

 float x,y; //lets say these are populated with a point/average on your swipe path
 View element = ... ; //view representing one item in your array
 if ( x > element.getX() && x < element.getX() + element.getWidth() ) {
     //Collides with x axis, check y axis now
        //if that also colides, save this element in a set/list
 }

When the user lifts his/her finger from the screen, all of the selected image views will be stored in the ArrayList.

Upvotes: 1

Related Questions