Mohamad Sharifi
Mohamad Sharifi

Reputation: 21

Android: Detect if user touches a view inside parent view

I have 40 ImageViews inside a GridLayout, they have different colors and I want to know if user touched the desirable image or somewhere else(for example if user touched red image). How should I do that?

Upvotes: 0

Views: 930

Answers (2)

Thibaud Renaux
Thibaud Renaux

Reputation: 280

If you manage your images in a collection maybe think about switching to gridview and implement an adapter with an itemClickListener. So depend on which item is clicked do you action. How GridLayout items come from the Adapter(List/Image) in Android App

Upvotes: 0

Florian Hansen
Florian Hansen

Reputation: 816

Set an OnClickListener for each view and store the views. In onClick you can check the view and know what ImageView was clicked. You could also think about changing the type to ImageButtons!

If you have further problems with your Grid not being able to be clicked, check this out: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/

TLDR: Disable focusing of the views in your layout.

Upvotes: 1

Related Questions