Android layout design: composite Relative vs fragments

112 views Asked by At

I want a simple scrollable ListView. I have a custom adapter that makes each row display a picture and some text.

Underneath I want a few buttons.

I'm looking for a layout like this sample picture:

Sample Layout

This seems like it should be trivial. I have a single main activity that extends ListActivity:

public class ShufflerMain extends ListActivity {
    private ArrayList<ListEntry> init = new ArrayList<ListEntry>();
    private Button btnAddEntry;
    private Button btnShuffle;
    private Button btnClearAll;
    private InitiativeArrayAdapter initAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initViews();
        loadDummyData();
        listAdapter = new MyCustomArrayAdapter(this, init);
        setListAdapter(initAdapter);
    }

This is fine and dandy, but I wanted to have a few buttons underneath this list. (to do things like add to the list, clear the list, etc). My main XML looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.myapp.ShufflerFragment" >

<ScrollView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ScrollView>

<Button
    android:id="@+id/clearAll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="@string/clearAll"/>

<Button
    android:id="@+id/add_new_entry"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/clearAll"
    android:text="@string/addEntry"/>

<Button
    android:id="@+id/shuffle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/add_new_entry"
    android:text="@string/shuffle"/>
</RelativeLayout>

I can run the app just fine, but the Graphical layout preview in Eclipse looks right and I see my 3 buttons. When I run it on my phone, all I see is my ListView populated with my dummy data. I added calls in the Java to pull down the buttons and add listeners, and findViewById is returning null on them.

Have I completely set up this project incorrectly? Do I need to have a different Activity as my "Main" separate from the ListView in order to have any capability other than a full-screen list?

Thanks.

1

There are 1 answers

2
Adam Fręśko On BEST ANSWER

Your scroll view is empty. If you want to have multiple items scrollable in it put a viewgroup layout inside in example a linear layout and then put button inside.

Your views are returning null mostlikly because you didn't setcontentview() in the on create method. Rather than extending listActivity extend normal activity and supply layout that have listView