Deepmala singh M
Deepmala singh M

Reputation: 399

How can i remove layout in android?

I have created multiple button in a layout dynamically.Now,how can i remove this layout after used.
for example:-

LinearLayout parent = new LinearLayout(this);

    parent.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    parent.setOrientation(LinearLayout.HORIZONTAL);

    for (int i = 0; i < 10; i++) {
        Button b = new Button(this);
        b.setText("Primary");

        Drawable image = ContextCompat.getDrawable(
                getApplicationContext(),
                R.drawable.your_image);
        image.setBounds(0, 0, 60, 60);

        b.setCompoundDrawables(null, null, image, null);
        parent.addView(b);
    }

Upvotes: 0

Views: 1771

Answers (6)

Preety Tilwani
Preety Tilwani

Reputation: 41

I had removed ParentView from its child (Textview's) click.

tvClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (llContainer.getChildCount() > 1) {
                    ((LinearLayout) view.getParent()).removeView(view);
                }
            }
        });

Upvotes: 0

Manoj Perumarath
Manoj Perumarath

Reputation: 10214

Try this:

 View button = view.findViewById(R.id.buttonid);
 ((ViewGroup) button.getParent()).removeView(button);

Upvotes: 0

Jay Pandya
Jay Pandya

Reputation: 138

get the parent of the view and remove that view from its parent

((ViewGroup) parent.getParent()).removeView(parent);

Upvotes: 0

Raymond
Raymond

Reputation: 2372

You'll have to use removeView(View) or removeViewAt(position) from the parent, depending on if you keep track of the indexes or the objects.

parent.removeView(button);
parent.removeViewAt(buttonIndex);

Upvotes: 0

Tulsi
Tulsi

Reputation: 719

you can set visibility of linearLayout to gone by parent.setVisibility(View.GONE);

or remove all the views from the linearLayout by parent.removeAllViews().

Upvotes: 0

Lino
Lino

Reputation: 6160

If you still have the instance of the linear layout, just call removeView(...) as you did for the addView(...).

Upvotes: 2

Related Questions