How in programming under Android (i. E. In Java with appropriate libraries) programmatically add an element (for example, a button) to the end of the container specified by id? That is, if there is already something in the container, the added element should be inserted at the end.

For those that own JavaScript : I now asked about the equivalent of the append() method of the jQuery library.

Probably, to answer my question, you just need to fix one line in the code:

 // Определяем контейнер по ID RelativeLayout Contnr = (RelativeLayout) findViewById(R.id.Contnr); // Создаём новый объект (например, textInput) EditText InputTextA = new EditText(this); // Добавляем его (данный метод добавит объект в начало контейнера, // как показал эксперимент, поверх уже существующих.) Contnr.addView(InputTextA); 

Just in case, I will point out once again that we do not know in advance if there is something in the container and if there is one, after which of the existing objects we will add a new one.

    1 answer 1

    RelativeLayout - layout with a relative arrangement of elements, it is necessary to strictly indicate the relationship of children, it is suitable for such virgin soil. I implemented the following behavior for such a task. LinearLayout created with a vertical orientation stretched to the screen (or container). When we add an item we do the following

    if the vertical LinearLayout empty - add a horizontal LinearLayout with the height and width of the WRAP_CONTENT to which the child is added

    when a horizontal LinearLayout is added - we add children to it until its width is less than the width of the parent vertical LinearLayout , as soon as the width is larger - we create a new horizontal LinearLayout and put an element into it and so on.

    Since the parent Layout vertical, it turns out that the horizontal ones go one by one from top to bottom and fill themselves evenly.

    Well, since it is done programmatically - we can always control the elements - what exactly is in the container, their width, etc.