Search code examples
androidandroid-layoutbuttondialogonclicklistener

How can I delete multiple button form view in material dialog In android?


When I delete first button from an UI of material dialog. But,it removes the last button from an UI.I want to delete multiple buttons from an UI.After every click event of a button.

layout2 = (LinearLayout) dialog.getCustomView().findViewById(R.id.layoutDisplayTags);
layout2.setOrientation(LinearLayout.VERTICAL); 
layout2.setWeightSum(1);
layout2.removeAllViews();
layout2.invalidate();


float rowneed = ((float) count2 / 5);
k = 0;
for (int i = 0; i < ceil(rowneed); i++) {
row2 = new LinearLayout(getContext());
row2.setLayoutParams(new LinearLayout.LayoutParam(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
row2.removeAllViews();
row2.invalidate();
 for (int j = 0; j < 5; j++) 
{
   btnTag2 = new Button(getContext());
   btnTag2.setHeight(15);
   btnTag2.setWidth(0);
   btnTag2.setMinimumWidth(155);
   btnTag2.setMinimumHeight(135);

      if (k < count2)
         {
             if (AllExpenseTagArray.size() != 0)
                {
                  btnTag2.setText(AllExpenseTagArray.get(k).getTagName());
                  btnTag2.setId(k);
                  btnTag2.setGravity(View.TEXT_ALIGNMENT_CENTER);
                  Drawable image = ContextCompat.getDrawable(getContext(), R.drawable.ic_close_black_18dp);
                  image.setBounds(1, 3, 25, 25);
                  btnTag2.setCompoundDrawables(null, null, image, null);
                  k++;
                  btnTag2.setVisibility(View.VISIBLE);
                }
             else {
                    btnTag2.setVisibility(View.INVISIBLE);
                    }
                 } 
         else {
                 btnTag2.setVisibility(View.INVISIBLE);
               }
               btnTag2.setTextSize(7);
               row2.addView(btnTag2);
             }

              layout2.addView(row2);
              layout2.invalidate();

 }

This code is not working

for (int id = 0; id < AllExpenseTagArray.size(); id++)
  {
     btnTag2 = (Button) dialog.getCustomView().findViewById(id);
     final int finalId = id;
     final int finalId1 = id;
     btnTag2.setOnClickListener(new View.OnClickListener() 
         {
            public void onClick(View view) 
               {
                    btnTag2.setVisibility(View.GONE);  
                    deleteId = AllExpenseTagArray.get(finalId1).getTagId();
               }                                                                                                                                        
         });

Solution

  • btnTag2 in your onClick listener refers to a global variable which is the same for all the listeners you are creating and is just the last Button that you create - that is why you are seeing what you are seeing. The view argument to the onClick listener is the Button, so you can make the following change to make a button to disappear when you click it.

            for (int id = 0; id < AllExpenseTagArray.size(); id++) {
            btnTag2 = (Button) dialog.getCustomView().findViewById(id);
            final int finalId = id;
            final int finalId1 = id;
            btnTag2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    view.setVisibility(View.GONE); // Make this change
                    deleteId = AllExpenseTagArray.get(finalId1).getTagId();
                }
            });
        }