Groot
Groot

Reputation: 493

Android - Removing an ImageView from a RelativeLayout

How can I remove an ImageView from a RelativeLayout? For some reason RelativeLayout.removeView(iv); doesn't work. I'm creating the ImageView programmatically like this:

final ImageView iv = new ImageView(MainActivity.this); But this doesn't work.

I don't want to use iv.setVisibility(View.GONE); because this doesn't really remove the image, it just sets the visibility.

Why isn't this working? Is there any alternative?

Upvotes: 0

Views: 978

Answers (1)

KOTIOS
KOTIOS

Reputation: 11194

Just a code snip below may help you understand how dynamic tag works :

   addBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.row, null);
                Button buttonRemove = (Button) addView
                        .findViewById(R.id.remove);
                addView.setTag(tag);
                buttonRemove.setTag(tag);
                dynamicLayoutsTags.add(tag);
                Container.addView(addView);
                tag++;
                buttonRemove.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // ((LinearLayout) addView.getParent())
                        // .removeView(addView);
                        Integer removeTag = (Integer) v.getTag();
                        View deleteView = Container.findViewWithTag(removeTag);
                        Container.removeView(deleteView);
                        dynamicLayoutsTags.remove(removeTag);
                    }
                });

            }
        });

Upvotes: 1

Related Questions