Pages

Empty ListView

When a ListView has no items it is possible to display a default View e.g. some explanatory text about why there is nothing to be seen.
 
The simplest way to do this is with a TextView called id/android:empty like this:
 
<ListView android:id="@+id/android:list" ></ListView>
<TextView android:text="blah blah blah" android:id="@+id/android:empty" ></TextView>
 
You can also set the empty view in code which gives you some more flexibility:
 
public void onCreate(Bundle savedInstanceState) {
Log.d(getClass().getSimpleName(), "onCreate");
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.buckets_list);
getListView().setEmptyView(findViewById(R.id.EmptyBucketLayout));
 
R.id.EmptyBucketLayout can then refer to a more complex viewgroup LinearLayout / TextView / Button type of idea:
 
<ListView android:id="@+id/android:list"></ListView>
  <LinearLayout android:id="@+id/EmptyBucketLayout">
    <TextView android:text="@string/welcome_heading"></TextView>
    <TextView android:text="@string/welcome_text"></TextView>
    <Button android:text="Create Bucket"></Button>
  </LinearLayout>
 
What I could not get to work (and I found someone else with the same problem) is inflating an abitrary view and using it in the setEmptyView method:
 
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View emptyView = inflater.inflate(R.layout.buckets_list_empty, null);
getListView().setEmptyView(emptyView);