How can I add & delete a view from a layout?
10 Answers
I've done it like so:
((ViewManager)entry.getParent()).removeView(entry);

- 31,285
- 16
- 80
- 91
-
49
-
1@schwiz how can we reset the view id's in this case.My problem is even after my activity finishes whenever the next view gets added the view id gets incremented.I want the view id to default to 1 when i get back to this activity again. – AndroidNewBee Nov 17 '15 at 12:40
-
-
ViewGroup extends View implements ViewParent, ViewManager. nicer to call it with casting to (ViewGroup) – Mercury Apr 25 '17 at 21:30
Use ViewStub and specify the layout of the view you want to toggle. To view:
mViewStub.setVisibility(View.VISIBLE) or mViewStub.inflate();
To disappear:
mViewStub.setVisibility(View.GONE);

- 56,972
- 13
- 121
- 140

- 21,813
- 7
- 42
- 56
-
46
-
If you want to hide/unhide you could use .setVisibility(View.INVISIBLE) and .setVisibility(View.VISIBLE). When the view is invisible the user can't interact with it. – Dennis van der Schagt Aug 28 '15 at 19:07
-
Let's say I have a textView with an onClickListener attached to it. If I make it disappear and then I tap on it, will the event still fire? – ahitt6345 Aug 04 '16 at 19:47
This is the best way
LinearLayout lp = new LinearLayout(this);
lp.addView(new Button(this));
lp.addView(new ImageButton(this));
// Now remove them
lp.removeViewAt(0); // and so on
If you have xml layout then no need to add dynamically.just call
lp.removeViewAt(0);

- 23,478
- 6
- 59
- 81

- 11,935
- 4
- 61
- 87
-
What will be the diff in terms of performance? I mean the performance diff of adding them dynamically or putting them into a static xml layout file and let it be inflated ? – stdout Jun 15 '16 at 13:24
To add view to a layout, you can use addView
method of the ViewGroup
class. For example,
TextView view = new TextView(getActivity());
view.setText("Hello World");
ViewGroup Layout = (LinearLayout) getActivity().findViewById(R.id.my_layout);
layout.addView(view);
There are also a number of remove methods. Check the documentation of ViewGroup. One simple way to remove view from a layout can be like,
layout.removeAllViews(); // then you will end up having a clean fresh layout

- 1,440
- 20
- 17
Great anwser from Sameer and Abel Terefe. However, when you remove a view, in my option, you want to remove a view with certain id. Here is how do you do that.
1, give the view an id when you create it:
_textView.setId(index);
2, remove the view with the id:
removeView(findViewById(index));

- 3,303
- 1
- 31
- 27
For changing visibility:
predictbtn.setVisibility(View.INVISIBLE);
For removing:
predictbtn.setVisibility(View.GONE);

- 677
- 7
- 21

- 2,719
- 1
- 28
- 28
-
It fixed my problem, more than every other answer. Useful if like me, your background or some params prevent the view to disappear totally. – Virthuss Oct 07 '15 at 01:50
-
1it is because View.GONE don't remove the view for the layout, only make it invisible, and it doesn't take any space for layout purposes. – Victor Mar 23 '16 at 15:28
you can use addView or removeView
java:
// Root Layout
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// TextView
TextView textView = new TextView(context);
textView.setText("Sample");
// Add TextView in LinearLayout
linearLayout.addView(textView);
// Remove TextView from LinearLayout
linearLayout.removeView(textView);
kotlin:
// Root Layout
val linearLayout = LinearLayout(context)
linearLayout.gravity = Gravity.CENTER
linearLayout.orientation = LinearLayout.VERTICAL
// TextView
val textView = TextView(context)
textView.text = "Sample"
// Add TextView in LinearLayout
linearLayout.addView(textView)
// Remove TextView from LinearLayout
linearLayout.removeView(textView)

- 11,234
- 1
- 68
- 78
Kotlin Reusable Extension Solution
Simplify removal
Add this extension:
myView.removeSelf()
fun View?.removeSelf() {
this ?: return
val parent = parent as? ViewGroup ?: return
parent.removeView(this)
}
Simplify addition
Here are a few options:
// Built-in
myViewGroup.addView(myView)
// Null-safe extension
fun ViewGroup?.addView(view: View?) {
this ?: return
view ?: return
addView(view)
}
// Reverse addition
myView.addTo(myViewGroup)
fun View?.addTo(parent: ViewGroup?) {
this ?: return
parent ?: return
parent.addView(this)
}
hi if are you new in android use this way Apply your view to make it gone GONE is one way, else, get hold of the parent view, and remove the child from there..... else get the parent layout and use this method an remove all child parentView.remove(child)
I would suggest using the GONE approach...

- 2,929
- 5
- 32
- 58
-
1GONE will effectively hide the view but it won't remove it from the layout – Sababado Nov 28 '12 at 16:38
-
1
-
You're right about that, but I was trying to hint that you weren't answering the question by providing the "GONE" solution. – Sababado Nov 29 '12 at 15:20
I am removing view using start and count Method, i have added 3 view in linear Layout.
view.removeViews(0, 3);

- 191
- 2
- 7