Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the ID of the clicked child view added dynamically to a LinearLayout?

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.

like image 903
hshed Avatar asked Sep 25 '12 21:09

hshed


People also ask

What is layout_ weight in android?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

How do I add a view in view?

Use the LayoutInflater to create a view based on your layout template, and then inject it into the view where you need it. LayoutInflater vi = (LayoutInflater) getApplicationContext(). getSystemService(Context. LAYOUT_INFLATER_SERVICE); View v = vi.


1 Answers

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
        }
    }
like image 152
triggs Avatar answered Oct 08 '22 22:10

triggs