0

I using extends ListActivity to show the empty message when the list are empty. Below is my xml file:

<?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:orientation="vertical"    >

    <ListView android:id="@+id/android:list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1.0">
    </ListView>

    <TextView android:id="@android:id/empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:text="No Items! Please Add!"/> 

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"        
        android:text="Add Item" />

</LinearLayout>

My problem is the button can only appear when there have item in list, but can't appear when the list is empty. How can the button appear when the list are empty?

user2301281
  • 757
  • 2
  • 12
  • 27

5 Answers5

1

At first check getCount(). If getCount() return 0, then the visibility of button should be visible otherwise 'invisible' or 'gone'

Android Boy
  • 4,335
  • 6
  • 30
  • 58
0

Just use the the android tag android:visible buttons codes in xml reference id you can set

button.setVisibility(View.Visible or View.inVisible) based on your requirement.
Abhijit Chakra
  • 3,201
  • 37
  • 66
0

You should create a footer view layout inside your listview's layout.

View footer = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);

ListView.addFooterView(footer);

Also check this link.

Community
  • 1
  • 1
0

You can check if the ListView doesn't have items in it by doing

listview.getAdapter().isEmpty

If it is, set the button's visibility to gone. Otherwise, visible

josephus
  • 8,284
  • 1
  • 37
  • 57
0

Well, i'm not trying to add the footer but just need to add a button. I change the xml as below then problem solved. Thanks everyone.^^

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
       >

    <ListView android:id="@+id/android:list"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1.0"
          android:layout_above="@+id/bAdd">
    </ListView>


    <TextView android:id="@android:id/empty"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:layout_above="@+id/bAdd"
               android:text="No Items! Please Add!"/> 

     <Button
        android:id="@+id/bAdd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"        
        android:text="Add Item" />



</RelativeLayout>
user2301281
  • 757
  • 2
  • 12
  • 27