chiranjib
chiranjib

Reputation: 5308

Add & delete view from Layout

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

Upvotes: 97

Views: 153976

Answers (10)

Gibolt
Gibolt

Reputation: 47097

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

Upvotes: 1

Rasoul Miri
Rasoul Miri

Reputation: 12222

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)

Upvotes: 1

Sanjay Goswami
Sanjay Goswami

Reputation: 191

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

view.removeViews(0, 3);

Upvotes: 0

Tofeeq Ahmad
Tofeeq Ahmad

Reputation: 11975

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

Upvotes: 23

Kai Wang
Kai Wang

Reputation: 3361

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

Upvotes: 7

Ameen Maheen
Ameen Maheen

Reputation: 2737

For changing visibility:

predictbtn.setVisibility(View.INVISIBLE);

For removing:

predictbtn.setVisibility(View.GONE);

Upvotes: 6

Abel Terefe
Abel Terefe

Reputation: 1470

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

Upvotes: 21

Zala Janaksinh
Zala Janaksinh

Reputation: 2927

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

Upvotes: 0

Nathan Schwermann
Nathan Schwermann

Reputation: 31493

I've done it like so:

((ViewManager)entry.getParent()).removeView(entry);

Upvotes: 228

Sameer Segal
Sameer Segal

Reputation: 22023

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

Upvotes: 28

Related Questions