Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old January 2nd, 2012, 02:36 PM   #1 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Detecting the ID of child view in LinearLayout

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?

szw21 is offline  
Reply With Quote
Sponsors
Old January 3rd, 2012, 02:07 AM   #2 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

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.
jiminaus is offline  
Last edited by jiminaus; January 3rd, 2012 at 02:00 PM. Reason: Removed unneccesary quote
Reply With Quote
Old January 3rd, 2012, 10:27 AM   #3 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
szw21 is offline  
Reply With Quote
Old January 3rd, 2012, 02:02 PM   #4 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

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?
jiminaus is offline  
Reply With Quote
Old January 3rd, 2012, 09:29 PM   #5 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
szw21 is offline  
Reply With Quote
Old January 4th, 2012, 12:14 AM   #6 (permalink)
New Member
 
Join Date: Dec 2011
Posts: 6
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay got it figured out now thanks. I also rewrote the LinearLayout as a separate class - much cleaner implmentation that way.
szw21 is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:21 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo