• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Detecting the ID of child view in LinearLayout

szw21

Lurker
I'm trying to figure out how to detect the object I've clicked on within a LinearLayout which itself is a child window within a gridview. I've tried a few methods which I thought would work but none of them work. Within the gridview I can detect the correct child window (which in this case is the Linear Layout) but I cannot figure out how to detect the object within the LinearLayout itself.

Any ideas?
 
At what level is your onClick handler? Typically if you want to react to a click of a particular object, you'd attach that code directly to that object's onClick handler and let Android figure out what object has been touched/clicked.
 
Thanks for the reply. I have an onItemClick handler for the gridview which returns the clicked childview - in this case if I click the LinearLayout I do get a view to it.

I tried setting up an onClick (via an onClickListener) for the object directly. I tried to create a view to the resource by using findViewById and then using it to register the callback. For some reason I'm getting a null pointer so I'm probably in the code where the object has not yet been created.

Would that be the correct way to do this? I think that's what you are suggesting if I understand what you wrote correctly.
 
That is what I'm suggesting.

How are you setting up your activity? Where you trying to use findViewById(int)? What kind of view are you trying to click on?
 
Most of the app is based on a gridview so I modeled my app on the GridView sample. So the callback for the gridview is contained in the main activity. In one of the grids I used a LinearLayout consisting of one TextView and 3 ImageViews which are meant to act as buttons for selecting new views. This is done in the AdpaterView class for the gridview.

I was trying to find the resource within the gridview's callback function since I assumed the view would be valid when it was called - but nesting the LinearLayout callback within the gridview callback sounds messy - I'm not sure if it would even work.

I'm not sure if the correct solution would be to do this within the AdpaterView class or create a new class for the LinearLayout and implement the callback in there.
 
Okay got it figured out now thanks. I also rewrote the LinearLayout as a separate class - much cleaner implmentation that way.
 
Back
Top Bottom