In a previous post I demonstrated the concept of “master pages” in Android. Essentially what this did was allow you to have a base layout so that you didn’t have to repeat the same layout on all views that had the same things at the top and bottom of the layout. In the comments someone asked how this could be applied to the ListActivity. I haven’t done much with Android the last few months and have not had the chance to use this activity type yet so I decided to look into it.

After throwing some simple pages together I am not sure the master page concept can be directly applied to this situation. The reason being is that any class the extends the ListActivity class needs to have a ListView with the id @android:id/list. If we want to have a base layout with items above and below then we don’t want to have the list in that view.

The alternative is to create a ListActivity with a customize layout. Looking at the documentation the requirements are very simple. As mentioned earlier your layout must include a ListView with an id of @android:id/list. Then in the onCreate you call setContentView and pass in the id of your layout. See below.

Here is the layout for my activity:

   1: <?xml version="1.0" encoding="utf-8"?>
   2: <LinearLayout xmlns:android=""
   3:     android:layout_width="wrap_content" android:layout_height="fill_parent"
   4:     android:orientation="vertical">
   5:     <ListView android:id="@android:id/list" android:layout_width="wrap_content"
   6:         android:layout_height="wrap_content" android:layout_weight="1" />
   7:     <LinearLayout android:orientation="horizontal"
   8:         android:layout_width="wrap_content" android:layout_height="wrap_content">
   9:         <Button android:layout_width="wrap_content" android:id="@+id/button1"
  10:             android:layout_height="wrap_content" android:text="Press Me" />
  11:         <Button android:layout_width="wrap_content" android:id="@+id/button2"
  12:             android:layout_height="wrap_content" android:text="Press Me" />
  13:     </LinearLayout>
  14: </LinearLayout>

And here is the activity.

   1: public class MyListView extends ListActivity {
   3:     Button button1, button2;
   5:     @Override
   6:     protected void onCreate(Bundle savedInstanceState) {
   7:         super.onCreate(savedInstanceState);
   9:         setContentView(R.layout.my_list_layout);
  11:         button1 = (Button) findViewById(;
  12:         button2 = (Button) findViewById(;
  14:         button1.setOnClickListener(button1Listener);
  15:         button2.setOnClickListener(button2Listener);
  17:         String[] names = new String[] { "Item1", "Item2", "Item3", "Item4",
  18:                 "Item5", "Item6", "Item7", "Item8" };
  19:         this.setListAdapter(new ArrayAdapter<String>(this,
  20:                 android.R.layout.simple_list_item_1, names));
  21:     }
  23:     private OnClickListener button1Listener = new OnClickListener() {
  25:         public void onClick(View v) {
  26:             String[] names = new String[] { "Item1", "Item2", "Item3", "Item4",
  27:                     "Item5", "Item6", "Item7", "Item8" };
  28:             setListAdapter(new ArrayAdapter<String>(getBaseContext(),
  29:                     android.R.layout.simple_list_item_1, names));
  30:         }
  31:     };
  33:     private OnClickListener button2Listener = new OnClickListener() {
  35:         public void onClick(View v) {
  36:             String[] names = new String[] { "Blah1", "Blah2", "Blah3", "Blah4",
  37:                     "Blah5", "Blah6", "Blah7", "Blah8" };
  38:             setListAdapter(new ArrayAdapter<String>(getBaseContext(),
  39:                     android.R.layout.simple_list_item_1, names));
  40:         }
  41:     };
  43: }

I added some buttons to show you how to change the items in the list by clicking one or the other. Obviously you would want to get your data from a data store of something other than a static list. As always I hope this helps and if anyone comes up with a way to have a base activity and layout I would be very interested in how you accomplished that. Here is the Android documentation for some more info.