Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to dynamically include a XML Layout?

I want to decompose my UI into several XML Layouts. The first one would be the main layout, and the other ones would be the content layouts.

I would like to be able to set which content_layout should be included dynamically at run-time, so I don't want to set a "layout="@+layout/content_layout" in my XML file.

Here are my layouts:

main_layout.xml:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="600dp"     android:layout_height="800dp" >      <TextView         android:id="@+id/title"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_alignParentTop="true" />      <include /> <!-- I WANT TO INCLUDE MY CONTENT HERE -->      <Button         android:id="@+id/cancelButton"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_centerHorizontal="true"         android:text="Cancel" />  </RelativeLayout> 

content_layout.xml:

<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/whatever"     android:layout_height="wrap_content"     android:layout_width="fill_parent" /> 

content_layout2.xml:

<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/whatever2"     android:layout_height="wrap_content"     android:layout_width="fill_parent" /> 

How can I do that?

Thanks!

like image 810
nbarraille Avatar asked Oct 28 '10 19:10

nbarraille


People also ask

How do I insert one XML file into another Android?

Probably you will have to set within the include tag the properties that sets where it has to be placed: android:layout_above, android:layout_toLeftOf, etc... Use fill_parent instead. I pasted my code and I am working with honeycomb, thats why I was using match_parent. But it should work with fill_parent.

Is it possible to include one layout definition in another?

To efficiently re-use complete layouts, you can use the <include/> and <merge/> tags to embed another layout inside the current layout. Reusing layouts is particularly powerful as it allows you to create reusable complex layouts.

How can use multiple layouts in one activity in Android?

You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like: if (Case_A) setContentView(R. layout.


2 Answers

<RelativeLayout android:id="@+id/rl" ... 

In your code:

// get your outer relative layout RelativeLayout rl = (RelativeLayout) findById(R.id.rl);   // inflate content layout and add it to the relative layout as second child // add as second child, therefore pass index 1 (0,1,...)  LayoutInflater layoutInflater = (LayoutInflater)          this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);     rl.addView(1, layoutInflater.inflate(R.layout.content_layout, this, false) );  
like image 76
Mathias Conradt Avatar answered Oct 14 '22 03:10

Mathias Conradt


You could try using a ViewStub and just change which layout it is going to inflate programatically.

This answer discusses using one ViewStub.

like image 39
Bryan Denny Avatar answered Oct 14 '22 03:10

Bryan Denny