1. Download our Official Android App: Forums for Android!

Apps Detecting the ID of child view in LinearLayout

Discussion in 'Android Development' started by szw21, Jan 2, 2012.

  1. szw21

    szw21 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    6
    Joined:
    Dec 22, 2011

    Dec 22, 2011
    6
    0
    5
    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?
     

    Advertisement

  2. jiminaus

    jiminaus Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    190
    Joined:
    Oct 27, 2011

    Oct 27, 2011
    190
    36
    63
    Sydney, Australia
    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.
     
  3. szw21

    szw21 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    6
    Joined:
    Dec 22, 2011

    Dec 22, 2011
    6
    0
    5
    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.
     
  4. jiminaus

    jiminaus Well-Known Member
    Rank:
    None
    Points:
    63
    Posts:
    190
    Joined:
    Oct 27, 2011

    Oct 27, 2011
    190
    36
    63
    Sydney, Australia
    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?
     
  5. szw21

    szw21 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    6
    Joined:
    Dec 22, 2011

    Dec 22, 2011
    6
    0
    5
    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.
     
  6. szw21

    szw21 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    6
    Joined:
    Dec 22, 2011

    Dec 22, 2011
    6
    0
    5
    Okay got it figured out now thanks. I also rewrote the LinearLayout as a separate class - much cleaner implmentation that way.
     

Share This Page

Loading...