Reputation: 511
I'm trying to set empty view when the listView is empty. This is my full XML Layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fondgris"
android:orientation="vertical" >
<!-- Header Starts -->
<RelativeLayout
android:id="@+id/head"
android:layout_width="fill_parent"
android:layout_height="35sp"
android:background="@layout/header_gradient"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<TextView
android:id="@+id/activity"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:scaleType="center"
android:text="List of projects"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="bold"
android:tint="#0E5EC7" />
<ImageButton
android:id="@+id/logout"
android:layout_width="30dp"
android:layout_height="35dp "
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="0dp"
android:background="@android:color/transparent"
android:onClick="logout"
android:scaleType="center"
android:src="@drawable/sortie"
android:tint="#FFFFFF" >
</ImageButton>
<ImageButton
android:id="@+id/home"
android:layout_width="35dp"
android:layout_height="35dp "
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/logout"
android:background="@android:color/transparent"
android:onClick="home"
android:scaleType="center"
android:src="@drawable/home" >
</ImageButton>
<ImageButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="35dp "
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/home"
android:background="@android:color/transparent"
android:onClick="back"
android:scaleType="center"
android:src="@drawable/back"
android:tint="#FFFFFF" >
</ImageButton>
</RelativeLayout>
<!-- Editext and ImageView for Search -->
<RelativeLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/inputSearch"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/bnSearch"
android:hint="Search..."
android:inputType="textVisiblePassword"
android:textSize="12sp" />
<ImageButton
android:id="@+id/bnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/inputSearch"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/bnClear"
android:background="@android:color/transparent"
android:src="@drawable/search" />
<ImageButton
android:id="@+id/bnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/inputSearch"
android:layout_centerInParent="true"
android:layout_marginRight="3dip"
android:background="@android:color/transparent"
android:onClick="ClearSearch"
android:src="@drawable/cancel" />
<Button
android:id="@+id/imageButtonAddProject"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:drawableLeft="@drawable/add"
android:onClick="addProject"
android:text=" Add Project"
android:textColor="@color/text_color"
android:textSize="12sp" />
</RelativeLayout>
<!-- List View -->
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="337dp"
android:layout_weight="0.74" >
</ListView>
<TextView
android:id="@+id/emptyList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="Liste vide "
android:textSize="12sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04"
android:orientation="horizontal" >
<Button
android:id="@+id/bnPrevious"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_marginLeft="30dp"
android:background="@android:color/transparent"
android:text="<< Prev"
android:textSize="12sp" />
<TextView
android:id="@+id/page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page"
android:textSize="12sp" />
<EditText
android:id="@+id/editpage"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textSize="12sp" />
<TextView
android:id="@+id/of"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="of "
android:textSize="12sp" />
<TextView
android:id="@+id/nbpage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
<Button
android:id="@+id/bnNext"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_marginRight="30dp"
android:background="@android:color/transparent"
android:text="Next >>"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
It has no effect, I did debugging and it enter in if part but without setting the list to empty, the list continue to appear ! I can't understand why, I appreciate your help.
final ProjectContainer container = gson.fromJson(resultat,
ProjectContainer.class);
final ListView lv = (ListView) findViewById(R.id.list);
TextView emptyList = (TextView) findViewById(R.id.emptyList);
// if list empty
if (container == null) {
lv.setEmptyView(emptyList);
emptyList.setText("No Projects Found");
}
Thanks in advance.
Upvotes: 2
Views: 18425
Reputation: 5085
Use RelativeLayout
Add your emptyList view first
Add you listView (emptyList must be right behind the listview)
If you got 0 items from json, set the visibility of listView to View.INVISIBLE.
Edit:
Ok, you need to change your XML a little bit.
enclose emptyList and list inside a RelativeLayout as follow.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
>
<TextView
android:id="@+id/emptyList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="There is no item"
android:textSize="25sp" />
<ListView
android:id="@+id/list" android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</RelativeLayout>
To show empty text, set listView visibility to GONE. Here is sample output on my side. First with listView visibility Visible and 2nd is with listview visibility GONE
Here if your full updated xml code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fondgris"
android:orientation="vertical" >
<!-- Header Starts -->
<RelativeLayout
android:id="@+id/head"
android:layout_width="fill_parent"
android:layout_height="35sp"
android:background="@layout/header_gradient"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<TextView
android:id="@+id/activity"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:scaleType="center"
android:text="List of projects"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="bold"
android:tint="#0E5EC7" />
<ImageButton
android:id="@+id/logout"
android:layout_width="30dp"
android:layout_height="35dp "
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="0dp"
android:background="@android:color/transparent"
android:onClick="logout"
android:scaleType="center"
android:src="@drawable/sortie"
android:tint="#FFFFFF" >
</ImageButton>
<ImageButton
android:id="@+id/home"
android:layout_width="35dp"
android:layout_height="35dp "
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/logout"
android:background="@android:color/transparent"
android:onClick="home"
android:scaleType="center"
android:src="@drawable/home" >
</ImageButton>
<ImageButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="35dp "
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/home"
android:background="@android:color/transparent"
android:onClick="back"
android:scaleType="center"
android:src="@drawable/back"
android:tint="#FFFFFF" >
</ImageButton>
</RelativeLayout>
<!-- Editext and ImageView for Search -->
<RelativeLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/inputSearch"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/bnSearch"
android:hint="Search..."
android:inputType="textVisiblePassword"
android:textSize="12sp" />
<ImageButton
android:id="@+id/bnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/inputSearch"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/bnClear"
android:background="@android:color/transparent"
android:src="@drawable/search" />
<ImageButton
android:id="@+id/bnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/inputSearch"
android:layout_centerInParent="true"
android:layout_marginRight="3dip"
android:background="@android:color/transparent"
android:onClick="ClearSearch"
android:src="@drawable/cancel" />
<Button
android:id="@+id/imageButtonAddProject"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:drawableLeft="@drawable/add"
android:onClick="addProject"
android:text=" Add Project"
android:textColor="@color/text_color"
android:textSize="12sp" />
</RelativeLayout>
<!-- List View -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="337dp"
android:layout_gravity="center"
android:layout_weight="0.74" >
<TextView
android:id="@+id/emptyList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="There is no item"
android:textSize="25sp" />
<ListView
android:id="@+id/list" android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView>
</RelativeLayout>
<!-- end of listview -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.04"
android:orientation="horizontal" >
<Button
android:id="@+id/bnPrevious"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_marginLeft="30dp"
android:background="@android:color/transparent"
android:text="<< Prev"
android:textSize="12sp" />
<TextView
android:id="@+id/page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Page"
android:textSize="12sp" />
<EditText
android:id="@+id/editpage"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:textSize="12sp" />
<TextView
android:id="@+id/of"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="of "
android:textSize="12sp" />
<TextView
android:id="@+id/nbpage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
<Button
android:id="@+id/bnNext"
android:layout_width="80dp"
android:layout_height="35dp"
android:layout_marginRight="30dp"
android:background="@android:color/transparent"
android:text="Next >>"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
And in code.
if (container == null) {
lv.setVisibility(View.INVISIBLE);
}
Hope it will be useful.
Upvotes: 23
Reputation: 11
When you have a layout which has a ListView and below it a RelativeLayout with GraphicsButtons for Icons such as delete etc, then the method I use to get the ListView to take up all but the bottom bit of the screen is (as I found out from this site) to have its height set at 0px and a weight of 1 plus leaving the other Views/RelativeLayouts with no weights. All well and good until you first show an empty ListView after which it doesn't appear again because even though it has items in it later on, its height was set on its first appearance.
I show the little bit of code (no Layouts included) that I use to get the bloody thing working…
public void listFiles() {
boolean mEmptyList;
mFileNames = mFile.list();
mFileCount = (mFileNames == null) ? 0 : mFileNames.length;
mEmptyList = (mFileCount == 0);
mFileChecked = mEmptyList ? null : new boolean[mFileCount];
View mEmpty;
if ((mEmpty = mListView.getEmptyView()) != null) {
if (mEmpty.getVisibility() != ( mEmptyList ? View.VISIBLE : View.GONE )) {
mEmpty.setVisibility( mEmptyList ? View.VISIBLE : View.GONE );
if (mFileCount > 0)
mListView.setMinimumHeight(((ViewGroup) mListView.getParent()).getHeight());
mListView.setVisibility( mEmptyList ? View.GONE : View.VISIBLE );
}
}
}
If this isn't in the constructor of the adapter then one then has to call
mListView.invalidateViews();
Upvotes: -1
Reputation: 7061
Below a snippet with an example
TextView emptyList = new TextView(getContext());
emptyList.setText("The list is empty");
ListView mListView = (ListView) findViewById(R.id.list_view);
((ViewGroup)mListView.getParent()).addView(emptyList);
mListView.setEmptyView(emptyList);
mListView.setAdapter(adapter);
Upvotes: 17
Reputation: 17115
Calling to setEmptyView will not make your list empty. It sets a View which the ListView show if it's Adapter is empty. Thus, after setEmptyView you must set Adapter which has no items in it.
Upvotes: -1
Reputation: 1303
Are you calling setEmptyView(View)
before setAdapter(ListAdapter)
?
I think the order of these operations is important (setEmptyView
must be called first), and that an adapter must be set for the empty view to be displayed.
Upvotes: 1
Reputation: 633
You don't have to do a null check before setting the empty view.. Simply set the emptyview to the ListView. List view will take care of showing the empty view when the adapter is passed with zero contents.
lv.setEmptyView(emptyList);
emptyList.setText("No Projects Found");
Upvotes: 2