2

I want that this code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >

<TextView android:id="@+id/logo" style="@style/logo"
    android:layout_alignParentTop="true"
    android:text="@string/logo" tools:context=".MainActivity" />

to start in every layout. How can I do that I wouldn't need to add this to every layout page? Like for example in PHP I would use <?=include("header.php");?> (just an example, actually it's bad practise, doesn't matter here). Thank you.

good_evening
  • 21,085
  • 65
  • 193
  • 298

3 Answers3

2

The <include> tag is what you want. Read about it in Re-using Layouts with <include/>

Rajesh
  • 15,724
  • 7
  • 46
  • 95
2

You can use the <include .../> tag in your layout to reuse your header where is needed. See here for an example.

AggelosK
  • 4,313
  • 2
  • 32
  • 37
2

Save your header in XML file and then include this XML as child layout in other layouts:

<include  layout="@layout/headerlayout" android:id="@+id/headerLayoutid"  ...  />

The headerlayout.xml is name of your above layout that should be defined in res/layout and you like that be shown as header in all layouts(layout="@layout/headerlayout"),also headerLayoutid is id of your headerlayout(in it's parent) and you can reference to it in your parent layouts or in your code.
You can override all the layout parameters. This means that any android:layout_* attribute can be used with the tag. Here is an example:

<include android:layout_width="fill_parent" layout="@layout/image_holder" />  

You can see more details in about include in this page.
Edit:
If you have problems in about finding views in included layout,see this questions,I hope these help you:
findViewById not working for an include?
Finding an view by id?

Community
  • 1
  • 1
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167