Luigi Tiburzi
Luigi Tiburzi

Reputation: 4325

Merge xml layout programmatically in Android

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

Upvotes: 3

Views: 4243

Answers (2)

Matt
Matt

Reputation: 5511

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);

Upvotes: 5

CommonsWare
CommonsWare

Reputation: 1006549

Inflate topbar.xml using LayoutInflater, putting the results into scroll:

getLayoutInflater().inflate(R.layout.topbar, scroll);

Upvotes: 6

Related Questions