How to toggle Android ListView Empty Text?
Asked Answered
O

3

10

So I have a ListView with an empty list catch in the XML. It works fine. I set the TextView on the ID to be the empty list for different cases, so I need to be able to programatically change that text.

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>
<TextView android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_data"
/>

I would like to have something like this but it won't work:

TextView empty = (TextView)listing.findViewById(android.R.id.empty);
empty.setText(R.string.no_display_data);

Any ideas?

Ottie answered 9/7, 2011 at 12:11 Comment(2)
does "empty.setText(R.string.no_display_data)" throw a NullPointerException?Achaea
Yep, it sure does. It seems to not be able to find the native empty id.Ottie
A
15

Assuming you are in a ListActivity, do

TextView empty = getListView().getEmptyView();
empty.setText(R.string.no_display_data);

you can also possibly do (edit - the following is not correct)

TextView empty = (TextView)listing.findViewById(R.id.empty); //remove android
empty.setText(R.string.no_display_data);
Achaea answered 9/7, 2011 at 21:24 Comment(3)
You rock... The first was right: TextView empty = getListView().getEmptyView(); empty.setText(R.string.no_display_data);Ottie
For other people's reference: If you are not inside a ListActivity and instead need to work with a ListView, assign "empty" with .setEmptyView(empty) on the ListView.Hartzel
Thanks, it worked but has a bad side effect, the empty TextView is visible all the time, isn't it suppose to be hidden automatically by ListView control when it populates items, and gets visible once it has no more items? I'm using it in a ListView with a custom ArrayAdapter.Hopfinger
S
3

I usually set the visibility of the list to View.INVISIBLE when it has no content. And when there is content it is set to View.VISIBLE (through the .setVisibility(int)-method).

See the Android reference.


Sorry - I misread the actual question. The answer is still somewhat useful though - it stays for now.

You need to make a change to the ID-declaration in your XML. To something on the form "@+id/empty" then you'll be able to use the second code snippet you provided.

UPDATE: You should call the .setEmptyView(View)-method on you ListView to enable the magic.

Salver answered 9/7, 2011 at 12:22 Comment(2)
That is one way, but I am looking for a way to just change text.Ottie
This way works nicely when you have a custom adapter and views.Myxomycete
D
0

while setting the adapter

lview.setEmptyView(rootView.findViewById(R.id.empty_text_view_deductions));

in your xml
     <ListView android:id="@+id/listView_deductions"
            android:layout_width="fill_parent"
            android:groupIndicator="@null"
            android:divider="@color/fc_background_light_gray_transparent"
          android:layout_below="@+id/linear_cost_ll"
            android:dividerHeight="0.1dp"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_height="match_parent"/>

       <TextView
        android:id="@+id/empty_text_view_deductions"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ellipsize="none"
        android:gravity="center"
        android:padding="20dp"
        android:singleLine="false"
        android:text="Just make a call to complete the setup for this SIM.Call anyone you wish"
        android:textColor="@android:color/darker_gray"
        android:textSize="@dimen/text_small"
        android:visibility="gone" />
Debouch answered 30/8, 2015 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.