How can I remove all View elements on the layout that have been added from the code (do not have an id ), without restarting the Activity ?

Any options will do. Thank you in advance.

    2 answers 2

    To remove all the View from the container widget (such as any Layout ) in the ViewGroup class, from which all container widgets inherit, there is a removeAllViews() method:

     LinearLayout layout = new LinearLayout(this); // или если контейнер на разметке LinearLayout layout = (LinearLayout) findViewById(R.id.layout); ... layout.removeAllViews(); // удалить все View из LinearLayout 

      You need the ViewGroup#removeAllViews() method - it will just remove all the child views of the one on which this method will be called

      • And this is a class method? - Ker_ Jen
      • @Ker_Jen is not a View method, but a ViewGroup (any layout) - pavlofff
      • @pavlofff I understand that this is not a static method and will have to create an instance of the ViewGroup class. But after all the onLayout method is implemented. How without him? - Ker_ Jen
      • Try to describe your problem in more detail by editing the question. It is necessary to create an instance, naturally. I don't understand what the problem is with onLayout () - pavlofff
      • The @pavlofff ViewGroup is abstract because there is an onLayout method that can be implemented. What he does, and why he is needed, just code with methods that are not used, I do not want to score. - Ker_ Jen