Add and Remove Views in Android Dynamically?


How do I add and remove views such as TextViews from Android app like on the original stock Android contacts screen where you press a small icon on the right side of a field and it adds or deletes a field which consists of a TextView and an editTextView (from what I can see).

Any examples on how to achieve this?

10/28/2012 6:21:53 AM

Accepted Answer

ViewParents in general can't remove views, but ViewGroups can. You need to cast your parent to a ViewGroup (if it is a ViewGroup) to accomplish what you want.

For example:

View namebar = View.findViewById(;
((ViewGroup) namebar.getParent()).removeView(namebar);

Note that all Layouts are ViewGroups.

7/26/2019 12:33:16 PM

I need the exact same feature described in this question. Here is my solution and source code: And you can see the video demo in action here:


Basically you'll two xml layout files:

  • A horizontal LinearLayout row view with a TextEdit, a Spinner and an ImageButton for deletion.
  • A vertical LinearLayout container view with just a Add new button.


In the Java code, you'll add and remove row views into the container dynamically, using inflate, addView, removeView, etc. There are some visibility control for better UX in the stock Android app. You need add a TextWatcher for the EditText view in each row: when the text is empty you need to hide the Add new button and the delete button. In my code, I wrote a void inflateEditRow(String) helper function for all the logic.

Other tricks

  • Set android:animateLayoutChanges="true" in xml to enable animation
  • Use custom transparent background with pressed selector to make the buttons visually the same as the ones in the stock Android app.

Source code

The Java code of the main activity ( This explains all the logic, but quite a few properties are set in xml layout files, please refer to the Github source for complete solution):

public class MainActivity extends Activity {
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// The "Add new" button
private Button mAddButton;
// There always should be only one empty row, other empty rows will
// be removed.
private View mExclusiveEmptyView;

public void onCreate(Bundle savedInstanceState) {
    mContainerView = (LinearLayout) findViewById(;
    mAddButton = (Button) findViewById(;

    // Add some examples

// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
    // Inflate a new row and hide the button self.

// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
    // remove the row by calling the getParent on button
    mContainerView.removeView((View) v.getParent());

// Helper for inflating a row
private void inflateEditRow(String name) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.row, null);
    final ImageButton deleteButton = (ImageButton) rowView
    final EditText editText = (EditText) rowView
    if (name != null && !name.isEmpty()) {
    } else {
        mExclusiveEmptyView = rowView;

    // A TextWatcher to control the visibility of the "Add new" button and
    // handle the exclusive empty view.
    editText.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable s) {

            // Some visibility logic control here:
            if (s.toString().isEmpty()) {
                if (mExclusiveEmptyView != null
                        && mExclusiveEmptyView != rowView) {
                mExclusiveEmptyView = rowView;
            } else {
                if (mExclusiveEmptyView == rowView) {
                    mExclusiveEmptyView = null;

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

    // Inflate at the end of all rows but before the "Add new" button
    mContainerView.addView(rowView, mContainerView.getChildCount() - 1);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow