0

Is it possible to inflate a LinearLayout from XML that contains some static objects and another LinearLayout and later when "XML" code is used inside the LinearLayout it's content is being added inside the inner LinearLayout.

Explanation with some code removed:

<LinearLayout id="main">
    <LinearLayout id="top">
        <TextView text="This is always here" />
        <ImageView src="@drawable/image_alwayshere" />
    </LinearLayout>
    <LinearLayout id="bottom">
    </LinearLayout>
</LinearLayout>

This is then inflated by my View "CustomLinearLayout" or whatever we call it and when using the View in another Layout:

<com.my.views.CustomLinearLayout>

    <ImageButton id="button1" src="@drawable/button1" />

</com.my.views.CustomLinearLayout>

In this case, the ImageButton should not be added below "bottom" it should be added into it. SO whatever I have in top, stays static and whatever I want to change is added to the Bottom LinearLayout.

Is this possible and if it is how could it be done?

Not sure if it's good or bad practice, if it would work. But if I have a constant layout (top container, middle container and bottom container) and I have 10 different activities and the only one changing content is the middle one, I can easily make one change to the top and bottom container at one place instead of 10 places and have whatever "View" I want to show in my activity be added inside.

Maybe I need to create a whole new ViewGroup for this? But currently working on LinearLayout since it's functionality is pretty much what I need.

If not, then what I'm looking for is where and when a LinearLayout reads the content of the XML and then override that to be added to my inner LinearLayout instead.

Deukalion
  • 2,516
  • 9
  • 32
  • 50

1 Answers1

-1

It is possible, you can either use include tag to add the other xml layout into the "bottom" layout and make its visibility as "gone" and change it to "visible" when you need it.

Or you can do that dynamically and inflate the xml whenever you want then add it to "bottom" using ViewGroup.addView(View) method.

Abdallah Alaraby
  • 2,222
  • 2
  • 18
  • 30
  • I'm not even certain where to look for this, all I had was an idea that I wanted to know if it worked. When does the LinearLayout obtain the code within XML and from there I guess it's pretty easy to just bottomLayout.addView(View) but from my Constructor I call a method that inflates this View first. Then then if something's been added to the XML code it gets added to the View too but it makes it weird at the moment. What you're refering to is probably to recreate a View from ViewGroup? I would lose all functionality of a LinearLayout which I'd prefer to have. – Deukalion Nov 05 '14 at 19:29
  • If you want to add views to any of your ViewGroups, all you have to do is `(ViewGroup) View.findViewById(yourViewGroupId)` then add your views to the resulting `ViewGroup` – Abdallah Alaraby Nov 05 '14 at 19:41
  • I'm confused. For simplicity: "Custom.xml" is a LinearLayout, then "Top" and "Bottom" LL inside it. Then CustomLL extends LinearLayout is created, after construction Inflating "Custom.xml" layout. Then, used on some View and adding an ImageButton inside it. Tried now, overriding the addView methods, checking the id of the "child" before - if it's one of the default id's its added as usual, while if it's not it's added to the "bottom" LinearLayout - shows nothing instead. – Deukalion Nov 05 '14 at 20:42