Viacheslav Alexsandrovich Viacheslav Alexsandrovich - 4 months ago 10
Java Question

How to create button delete view in loop for Android Studio

I have a loop for where I create all views and in this loop I have button where I am trying to create delete 1 view if a button has been selected. All the view is in frames and has a linearlayout with a scrollview.

FrameLayout.LayoutParams deletePartsParams = new
FrameLayout.LayoutParams(tenPixelsWight*7,tenPixelsHeight*7);
deletePartsParams.setMargins(tenPixelsWight * 60, tenPixelsHeight * 20, 0, 0);
deleteOfPart.setBackgroundResource(R.drawable.delete); //Кнопка удалить автозапчасть
deleteOfPart.setClickable(true);
deleteOfPart.setTag(i);

deleteOfPart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});

deleteOfPart.setLayoutParams(deletePartsParams);
mainFrame.addView(deleteOfPart);

Answer
int count = mainLinearLayout.getChildCount();
View view = null;
for(int countI=0; countI<count; countI++) {
    view = layout.getChildAt(countI);
    if(((int) view.getTag()) == ((int) v.getTag())){
        mainLinearLayout.removeView(view);
        break;
    }
}

add above code into onClick of deleteOfPart