Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: set empty view to a list view

 <TextView
            android:id="@android:id/empty"
            style="@style/FacebookFont"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/no_result_found"
            android:textColor="@color/white" />

this is the xml code for empty view. I am setting empty view as follows:

    list.setAdapter(adapter);
    list.setEmptyView(findViewById(android.R.id.empty));

I have the items in the listview even then also before listview is populated the empty view is shown. I don't want that empty view to be shown when listview contains some item.

Please help.

like image 499
user2159624 Avatar asked Apr 11 '13 13:04

user2159624


People also ask

How check ListView is empty or not in android?

just check if (cartlist. size()<0) then yours list is Empty.!

How do I create a custom list view?

What is custom listview? Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Is ListView deprecated android?

It's worth to mentioned that the ListView is a kind of deprecated because the RecyclerView was introduced with the API 21 (Android Lollipop).

What is a custom list view?

Android Custom ListView (Adding Images, sub-title)After creating simple ListView, android also provides facilities to customize our ListView. As the simple ListView, custom ListView also uses Adapter classes which added the content from data source (such as string array, array, database etc).


1 Answers

if you are using ListActivity then no need to set the empty view manually. Remove list.setEmptyView(findViewById(android.R.id.empty)); from your code.

Change your layout like below then it's automatically add EmptyView if list has no item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="8dp"
        android:paddingRight="8dp">

    <ListView android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00FF00"
            android:layout_weight="1"
            android:drawSelectorOnTop="false"/>

     <TextView android:id="@android:id/empty"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:text="No data"/>

</LinearLayout>
like image 64
Dhaval Parmar Avatar answered Oct 28 '22 16:10

Dhaval Parmar