I have a tabbed layout and an activity using tabs as views. It has three tabs as ListViews
. If either of the lists is empty I want to show a simple TextView
You can solve this by adding an empty item when the list is empty. The following example is based on your pre-EDIT code:
FrameLayout frameLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
mListView_top10 = (ListView)findViewById(R.id.Top_10);
if (TopWR.size() == 0) {
// add a single item to the array, in order to indicate the list is empty
TopWR.add("The List Is Empty");
}
mListView_top10.setAdapter(new ArrayAdapter<String>(this, R.layout.listview_row,TopWR));
If you want to get fancier with how the empty list is displayed you can replace ArrayAdapter with a custom adapter. You can also use a different adapter when the list is empty.
I would insist you to use ViewStub
here with a ListView
inside a FrameLayout. When your ListView has data you can use VIEW.GONE
to ViewStub
and if your ListView is no data then use VIEW.VISIBLE
for the ViewStub. You can download example from github and get it working.
Have you tried setting the Visibility on each of the ListViews in your java code?
TextView empty = (TextView) findViewById(R.id.blank);
FrameLayout frameLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
mListView_top10 = (ListView)findViewById(R.id.Top_10);
if(TopWR.size()!=0) {
mListView_top10.setVisibility(View.VISIBLE);
mListView_top10.setAdapter(new ArrayAdapter<String>(this,
R.layout.listview_row,TopWR));
}
else {
mListView_top10.setVisibility(View.GONE);
empty.setVisibility(View.VISIBLE);
frameLayout.addView(empty);
}
TextView
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/blank"
android:visibility="gone"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No records Avaible"
android:textColor="#ffffff" />
Just call setEmptyView(...) on your ListView
, passing in the TextView
as argument.
TextView empty = (TextView) findViewById(R.id.blank);
mListView_top10.setEmptyView(empty);
The ListView
should automatically take care of toggling the visibility of the 'empty' view.
Tip: since the argument is a generic View
, you can pass in any subclass of View
, or more complex view hierarchy.
On a side note: you will probably have to give each ListView
its own TextView
instance as empty view to avoid clashing scenarios; e.g. when one list does have content, while another doesn't.