Reputation: 2135
I have a
EditText
with ImageView Plus
.
On click of ImageView Plus
I'm inflating new layout with EditText
and ImageView Minus
.
Now, On Click of ImageView Minus
. I want to remove the inflated layout. How to do this?
ArrayList<View> viewList;
/* Inflating new Layout */
case R.id.ivPlus:
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView = layoutInflater.inflate(R.layout.add_edit, null);
viewList.add(addView);
ll.addView(addView);
break;
/* Removing the inflated layout */
for (int i = 0; i < viewList.size(); i++) {
final int j = i;
ImageView minus= (ImageView ) viewList.get(j).findViewById(R.id.ivMinus);
minus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout extra_add = (LinearLayout) findViewById(R.id.lin_add);
extra_add.removeViewAt(j);
}
});
}
Upvotes: 2
Views: 166
Reputation: 883
Try this, just replace your method like this and enjoy
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_add:
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View addView = layoutInflater
.inflate(R.layout.inflate_layout, null);
viewList.add(addView);
lin_layout.addView(addView);
Button remove = (Button) addView.findViewById(R.id.button_remove);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lin_layout.removeView((View) v.getParent());
viewList.remove((View) v.getParent());
}
});
break;
}
}
Hope this helps and enjoy your work.
Upvotes: 1