DroidLearner
DroidLearner

Reputation: 2135

How to remove View in Android?

I have two Buttons which is in main layout. If Add Button is clicked it has to add EditText dynamically(Using Inflater). This part is working. If Remove Button is clicked it has to remove those EditText with last in first out.But I don't know how to remove views.

add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.add_edit, null);
            ll.addView(view);
        }
    });
    remove.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            ll.removeViewAt(0);
        }
    });

Upvotes: 2

Views: 7909

Answers (1)

Budius
Budius

Reputation: 39836

use that to remove the last in:

ll.removeViewAt(ll.getChildCount() - 1 );

alternatively, you can during inflation "remember" the views to remove

ArrayList<View> viewList = new ArrayList<View>();

// during inflation
view = layoutInflater.inflate(R.layout.add_edit, null);
viewList.add(view);
ll.addView(view);

// then to remove the last
view  = list.get(list.size() - 1);
ll.removeView(view);

Upvotes: 5

Related Questions