Reputation: 399
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
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
Reputation: 10214
Try this:
View button = view.findViewById(R.id.buttonid);
((ViewGroup) button.getParent()).removeView(button);
Upvotes: 0
Reputation: 138
get the parent of the view and remove that view from its parent
((ViewGroup) parent.getParent()).removeView(parent);
Upvotes: 0
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
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
Reputation: 6160
If you still have the instance of the linear layout, just call removeView(...)
as you did for the addView(...)
.
Upvotes: 2