Reputation: 4701
I am creating a LinearLayout
dynamically. I need to delete the LinearLayout
on LongPress
event.
My code :
public void addTileView(View v) {
_parentLayout = (LinearLayout) findViewById(R.id.gridCont);
View child = getLayoutInflater().inflate(R.layout.customtileview,null);
((TextView)child.findViewById(R.id.tileText)).setText("Tile View :"+_tileViewCount++);
_parentLayout.addView(child);
_parentLayout.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
return true;
}
});
}
How to do this ?
Upvotes: 1
Views: 861
Reputation: 4701
This works for me.
public void addTileView(View v) {
_parentLayout = (LinearLayout) findViewById(R.id.gridCont);
child = getLayoutInflater().inflate(R.layout.customtileview,null);
((TextView)child.findViewById(R.id.tileText)).setText("Tile View :"+_tileViewCount++);
_parentLayout.addView(child);
child.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
_parentLayout.removeView(v);
return true;
}
});
}
Thanks
Upvotes: 0
Reputation: 94
Its better to remove the view which you added in the parent . Say Parent layout is the main layout and child layout is the one which you want to remove. You should try parent_layout.removeView(child_layout);
removeAllViews() - will remove all the views inside the view , but not the main view .
Please refer to ViewGroup ViewGroup vg = (ViewGroup)(myView.getParent()); vg.removeView(myView);
Alternatively , You can make the visibility of your view to Visible.GONE , and then make it visible when required.
Upvotes: 2
Reputation: 2955
try this,
_parentLayout.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
parentLayout.removeAllViews();
return true;
}
});
Upvotes: 3