How to create a header-content layout in Android?

Another possible solution is using ViewFlipper or ViewSwitcher as the 'Content' section, where you include the various types of content you want to be able to flip between, and just set which view you want to display (you can do animations on flip/switch also). The downside to this is your content has to be added in the main layout, which each view being a child of the ViewFlipper/ViewSwitcher (can use or too, to keep the layouts in separate xml files).

Delegating the rendering of the switched content to another activity is not as straight forward. TabHost/TabWidget does allow that, where the content area of the tab host is generated from another activity. You might be able to use the Tab code as an example if you need to accomplish it that way.


You can create a layout file for your header. At the top of the layout for each content activity, include the layout file like:

<include android:id="@+id/header"
         layout="@layout/my_header"
         android:layout_height="wrap_content"
         android:layout_width="fill_parent"/>

How exactly you get the header to stay at the top of the content's layout will vary based on the rest of your layout. You could use a LinearLayout with orientation="vertical", or a relative layout with align_parentTop="true" on your include statement.

This android documentation has a good summary of basic layout types.

Tags:

Android