views:

91

answers:

1

If a listview is not given any items, how is it rendered? Will it still expand to fill the space in the layout allotted to it?

+3  A: 

Yes, it will still fill the space, but instead of displaying a list you can have it display another view. Here's an example:

<FrameLayout
    android:id="@+id/GLFrame"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" />
    <TextView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:id="@android:id/empty"
        android:gravity="center_vertical|center_horizontal|center"
        android:text="@string/no_songs_found"
        android:textColor="#FFF"
        android:textSize="20sp"
        android:textStyle="bold" />
    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/list"
        android:cacheColorHint="#0000" />

The key here is the "@android:id/empty" and "@android:id/list" ids. These tells Android that it should either display the list or the other view if the list is empty.

CaseyB
This only works if you are using `ListActivity`.
CommonsWare
That is true. I forgot to mention that, thanks!
CaseyB
BTW, if my main view layout contains a listview that is just *part* of the layout (i.e. there are other widgets above and below it), should I still extend ListView or should I enscapsulate that some other way?
Eno
You can still use a ListActivity.
CaseyB