26

How to include a xml data into an other xml data?

I have a header xml-file for my application which I want to use in my special other content xmls. Is there a ways to include the header into my content?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Xetoxyc
  • 325
  • 1
  • 3
  • 10

3 Answers3

37

use include tag

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"     
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 >
  <!-- Header -->
  <include
    android:id="@+id/container_header_lyt"  
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_above=...
    android:layout_toLeftOf=...
    layout="@layout/header_logo_lyt" //Name of the xml layout file you want to include
    />     

...

</RelativeLayout>
Axel M. Garcia
  • 5,138
  • 9
  • 27
  • 29
  • I haven't tried with a RelativeLayout but it should. 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... – Axel M. Garcia Jun 10 '11 at 13:34
  • it dont work for me with realtive layout may my error is somewhere else ill post my code – Xetoxyc Jun 10 '11 at 13:38
  • I have edited the answer to adapt it to the RelativeLayout case – Axel M. Garcia Jun 10 '11 at 13:38
  • match_parent isnt allowed for RelativeLayout – Xetoxyc Jun 10 '11 at 13:40
  • 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. Play a bit with your code. – Axel M. Garcia Jun 10 '11 at 13:44
  • mhh it work now no exception anymore but it only display my header not my content... – Xetoxyc Jun 10 '11 at 13:52
  • @Tobi play a bit with your layouts. You already know how to include a xml into an other xml. If only your header is being showed is because probably you are not setting well your layout attributes. This is a bit tricky at the beggining but is a matter of time. Check the attributes of the layout you are including, the attributes of the container layout, and play with relativelayout attributes.. etc. – Axel M. Garcia Jun 10 '11 at 14:04
12

You have to declare your "body" xml layout with a < merge > tag to use < include > tag on your principal layout.

<?xml version="1.0" encoding="utf-8"?>
<merge>
    <ImageView android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center"
        android:src="@drawable/image" / >

    <TextView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal|bottom"
        android:background="#AA000000"
        android:textColor="#ffffffff"
        android:text="Some Text" / >
</merge>

This is the content of your < include > tag

PipiBadenas
  • 245
  • 1
  • 3
  • 10
6

You should use < include > tag: Re-using Layouts with

denispyr
  • 1,403
  • 3
  • 21
  • 34
woodshy
  • 4,085
  • 3
  • 22
  • 21