Add buttons to a listactivity

10,725

From http://developer.android.com/reference/android/app/ListActivity.html:

“ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list"”

EDIT: here is an example:

The ListActivity may be created like this:

public class ListViewTest extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] values = {"One", "Two", "Three"};

        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, values));

        setContentView(R.layout.main);
    }
}

The main.xml layout is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:id="@android:id/list"></ListView>
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal"
        android:text="Test button" 
        android:id="@+id/TestButton"></Button>
</LinearLayout>
Share:
10,725

Related videos on Youtube

Roland
Author by

Roland

Trying not to be religious over techical choices and therefore always trying to learn things where my knowleadgemap has its white areas, instead of increasing the resolution. Therefore I know both Java and .Net and some other stuff too.

Updated on May 16, 2022

Comments

  • Roland
    Roland almost 2 years

    I have a layout for a ListActivity. To modify the list I have used menu-options. But to remove a couple of "clicks" on the screen I'd like to add two buttons in the button of the screen that is always visible and not affected if the list is scrolled.

    My problem is that I don't know how to add these buttons. I have tried a couple of solutions but the best I managed either the list or the buttons disapears from the layout. Seems that I can't get both buttons and list visible at the same time.

    So my question is how to create a layout where I can have both buttons and the list?

    Thanks in advance Roland

  • Roland
    Roland over 13 years
    I have a custom layout, but I can't get it to show anything else but the listview itself, or the other component, but not both. So I'd like to see an exampel that actually works.
  • ChrisJ
    ChrisJ over 13 years
    I've edited my answer so that it includes an example. I've just run the example -- works like a charm.
  • Zelimir
    Zelimir over 13 years
    How to create list activity with button in each row you can see from stackoverflow.com/questions/3750380/…
  • Lion789
    Lion789 about 10 years
    This is great! Thanks
  • Universe
    Universe about 9 years
    I had the same problem and looking for a solve for a lot of days, thanks!