12

I am adding a child view to a Linear Layout. The child views itself has some textview and imageviews in a Relativelayout. The child view is added dynamically in the LinearLayout on clicking a button. Right now I am able to add the child view as shown in this pic. http://dl.dropbox.com/u/50249620/SC20120926-031356.png what I have to do is uniquely identify which child view has been clicked in order to show appropriate actions. My code where I am adding the child view.

addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


                customView1 = inflater.inflate(R.layout.people, null);

                peopleName = (TextView) customView1.findViewById(R.id.peopleName);

                peopleName.setText(autoComplete.getText());
                customView1.setId(peopleInvitedRelativeLayout.getChildCount() + 1);

                params4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

                customView1.setLayoutParams(params4);
                peopleInvitedRelativeLayout.addView(customView1, params4);              

            }
        }); 

Any help or suggestions would be appreciated. Thanks.

biegleux
  • 13,179
  • 11
  • 45
  • 52
hshed
  • 657
  • 2
  • 8
  • 21
  • How about this link? http://stackoverflow.com/questions/7807058/get-child-view-in-a-relativelayout – flashsnake Sep 25 '12 at 21:54
  • I can get the id if I know the index of the child view in the parent LinearLayout, but I am not able to get the index of the child clicked by the user. – hshed Sep 25 '12 at 21:57
  • In an `onClick()` event, the `View` clicked is passed as a parameter. You can just call `getId()` on that view in the click event. – Kevin Coppock Sep 25 '12 at 22:09
  • triggs i have a similar doubt. please help: http://stackoverflow.com/questions/17061833/androidone-button-id-many-buttons-one-view-id-using-gettag-settag – user2468835 Jun 14 '13 at 07:47

1 Answers1

14

You can add a custom tag to any view simply by doing the following when you create the view

view.setTag(Object o);

then later in the onClickListener find the tag with

view.getTag()

setTag(Object o) will accept any kind of object be it a string, int or custom class

EDIT

addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);


            customView1 = inflater.inflate(R.layout.people, null);

            peopleName = (TextView) customView1.findViewById(R.id.peopleName);

            peopleName.setText(autoComplete.getText());
            customView1.setId(peopleInvitedRelativeLayout.getChildCount() + 1);

            params4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            customView1.setLayoutParams(params4);
            peopleInvitedRelativeLayout.addView(customView1, params4);

            //add a tag to a view and add a clicklistener to the view
            customView1.setTag(someTag);
            customView1.setOnClickListener(myClickListner);



        }
    });

the clicklistener - create a class variable for it

OnClickListener myClickListener = new onClickListener(){
    @Override
    public void onClick(View v) {

        if(v.getTag() == someTag){
             //do stuff
        }else if(v.getTag() == otherTag){
             //do something else
        }
    }
triggs
  • 5,890
  • 3
  • 32
  • 31
  • When I use customview1.setonclicklistener ..... , customview1 is null, because it has been inflated in button.setonclicklistener. And because of that I am not able to identify which child view has been clicked. – hshed Sep 26 '12 at 05:16
  • Then add the onClickListener to customView1 in the addButton onClickListener, I'll edit my answer with further details. – triggs Sep 26 '12 at 07:11
  • Thanks for your help. It seems to be working. I have used OnLongClickListener. I have to show context menu on long clicking an item. One of the context menu item has to remove the clicked item, which is not working with my code. Suppose I have to remove the first item public boolean onContextItemSelected(MenuItem item) { if(item.getTitle()=="Action 1"){ peopleInvitedRelativeLayout.removeViewAt(0); } else {return false;} return true; } where Action 1 is the context menu item. the removeView thing doesn't work. – hshed Sep 26 '12 at 09:08