The title of this post may be a little confusing because there aren’t master pages in Android. I have done a lot of development in Asp.Net which has the concept of master pages. A master page in Asp.Net allows you to put content that will be the same into the master page file and use place holders to the content that will be different for each page. The runtime then combines the master page with the requested page at runtime.

I have been working on a project for a client and the wanted their logo to be at the top of every activity. Since there were more than a couple activities I wanted have something like this master page functionality just in case they didn’t like the way it looked and wanted to change it. The solution was pretty easy. To start I created an a base Activity and derived it from the activity class. Then I overrode the onCreate and setContentView methods. In the onCreate I called the parents onCreate method and assigned any layout objects that needed to be used. In the setCotentView I created an inflater class and used it to inflate the view they passed in into a layout object on my base layout. My base layout only had a few LinearLayouts and an image for the logo. Then in my other activities I had them derived from my base Activity instead of the normal Activity class. When these activities called setContentView they were really causing their layout to be placed inside of my base layout. That was all there was to it. The only thing I noticed so far was that when requesting a progress bar (requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)) this needs to be done before calling super.onCreate. I think this is because nothing can be drawn yet before calling this function. The code for my base activity and layout are below.

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <LinearLayout
   3:       xmlns:android="http://schemas.android.com/apk/res/android"
   4:       android:layout_width="fill_parent"
   5:       android:layout_height="fill_parent"
   6:       android:orientation="vertical"
   7:       android:background="#000000"
   8:       android:padding="15dp" >
   9:       <LinearLayout android:orientation="horizontal" android:background="#000000"
  10:             android:layout_width="fill_parent" android:layout_height="wrap_content"
  11:             android:minHeight="50dp" android:paddingLeft="10dp">
  12:             <ImageView android:layout_width="wrap_content" android:id="@+id/ImageView01"
  13:                 android:adjustViewBounds="true" android:layout_height="wrap_content"
  14:                 android:scaleType="fitCenter" android:maxHeight="50dp" />
  15:     </LinearLayout>
  16:     <LinearLayout android:id="@+id/linBase"
  17:       android:layout_width="fill_parent"
  18:         android:layout_height="fill_parent" >
  19:     </LinearLayout>      
  20: </LinearLayout>

and the code for the activity:

   1: public class BaseActivity extends Activity {
   2:  
   3:     ImageView image;
   4:     LinearLayout linBase;
   5:     
   6:     public void onCreate(Bundle savedInstanceState) {
   7:         super.onCreate(savedInstanceState);
   8:         
   9:         super.setContentView(R.layout.base_layout);
  10:         
  11:         image = (ImageView)findViewById(R.id.ImageView01);
  12:         image.setImageResource(R.drawable.header);
  13:         
  14:         linBase = (LinearLayout)findViewById(R.id.linBase);
  15:     }
  16:     
  17:     @Override
  18:     public void setContentView(int id) {
  19:         LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  20:         inflater.inflate(id, linBase);
  21:     }
  22:         
  23: }

To use this you simply have you class derived from your base Activity

   1: public class SomeActivity extends BaseActivity {
   2:     
   3:     public void onCreate(Bundle savedInstanceState) {
   4:         super.onCreate(savedInstanceState);
   5:         
   6:         super.setContentView(R.layout.some_layout);
   7:         
   8:         //rest of code
   9:     }
  10:         
  11: }

This worked great for me and hopefully you will find this useful in your own coding.