Merge xml layout programmatically in Android

android, android-inflate, android-layout, xml

Solution

ScrollView can only have one direct child.

So you have to do something like this:

<ScrollView>
    <LinearLayout android:id="@+id/foo" android:orientation="vertical">
        <!--  youll add topbar here, programmatically -->
        <other things/>
    </LinearLayout/>
</ScrollView>

And then at runtime, you'll inflate topbar

View topbar = getLayoutInflater().inflate(R.layout.topbar, null);

and add it as the first index in `foo`

foo.addView(topbar, 0);

Problem

I have build a layout programmatically in Android (a treeview) and now I'd like to add to the built view a topbar (topbar.xml). So what I need is instead of: ``` setContentView(scroll) ``` Something like: ``` inflateInMyViewCalledScroll(topbar.xml) setContentView(scroll) ``` Thanks for your suggestions

Original source