97

How can I add & delete a view from a layout?

chiranjib
  • 5,288
  • 8
  • 53
  • 82

10 Answers10

229

I've done it like so:

((ViewManager)entry.getParent()).removeView(entry);
Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91
  • 49
    In the newer SDKs its `(ViewGroup)` :) – Ron Nov 13 '12 at 09:45
  • 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
  • Thanks a Lot for gave this answer – hem Dec 17 '16 at 10:24
  • ViewGroup extends View implements ViewParent, ViewManager. nicer to call it with casting to (ViewGroup) – Mercury Apr 25 '17 at 21:30
27

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);
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
Sameer Segal
  • 21,813
  • 7
  • 42
  • 56
23

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);
vallentin
  • 23,478
  • 6
  • 59
  • 81
Tofeeq Ahmad
  • 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
21

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
Abel Terefe
  • 1,440
  • 20
  • 17
7

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));
Kai Wang
  • 3,303
  • 1
  • 31
  • 27
6

For changing visibility:

predictbtn.setVisibility(View.INVISIBLE);

For removing:

predictbtn.setVisibility(View.GONE);
Rafael Almeida
  • 677
  • 7
  • 21
Ameen Maheen
  • 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
  • 1
    it 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
1

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)
Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
1

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)
}
Community
  • 1
  • 1
Gibolt
  • 42,564
  • 15
  • 187
  • 127
0

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...

Zala Janaksinh
  • 2,929
  • 5
  • 32
  • 58
0

I am removing view using start and count Method, i have added 3 view in linear Layout.

view.removeViews(0, 3);

Sanjay Goswami
  • 191
  • 2
  • 7