[android] Add & delete view from Layout

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

This question is related to android

The answer is


For changing visibility:

predictbtn.setVisibility(View.INVISIBLE);

For removing:

predictbtn.setVisibility(View.GONE);

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

view.removeViews(0, 3);


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

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)

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);

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


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));

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);

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)
}