Remove items from a Linear Layout Android Studio

0

I have a Linear Layout in Andorid Studio and I want that by pressing a button all the elements it contains are deleted. This is the code that I have, it deletes the elements correctly but when in the same app I reinsert other elements dynamically those that I had previously deleted appear (I do not know if I explain myself). I leave the code:

borrarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layout.removeAllViewsInLayout();
            }
        });

Thanks in advance.

    
asked by TaD 03.11.2018 в 22:01
source

2 answers

1

I think you should delete it this way:

borrarButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (layout.getChildCount() > 0) 
            layout.removeAllViews();
    }
});

Good luck!

    
answered by 03.11.2018 в 23:15
0
  

removeAllViewsInLayout () Called by a ViewGroup subclass for   eliminate secondary views of yourself, when you must first   Know your size on the screen before you can calculate how many   Secondary views will represent.

If you call only removeAllViewsInLayout will not work, you should also call

  

requestLayout () Call this when something has changed   invalidated the design of this view.

and

  

invalidate () Forces to redraw the views.

Do therefore the following:

borrarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                layout.removeAllViewsInLayout();

                layout.requestLayout();
                layout.invalidate();
            }
        });
    
answered by 04.11.2018 в 05:08