2

I am a new to Android stuff. I am creating an application that contains a list view that will get dynamically populated. My requirement is when the list is empty, I would like to show a message. I don't want to create additional views just for displaying this message. Is there any nice way to do this? Any suggestions?

2 Answers 2

6

ListActivity has a feature where it shows a specific view when the list is empty: @android:id/empty. You can easily just use a custom layout with ListActivity to achieve this.

If you don't want to use ListActivity you can always look at the implementation of ListView.java and adapt that.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot Alexander. This was exactly the same feature that I was looking for.
@alexanderblom ListView.java link is broken. Please fix it.
1

There really isn't a way to do it without creating extra views.

If your List view item is just a TextView you can always add an item to your adapter that says "the list is empty" and just make sure you clear the adapter before you add the real results.

Or you can add a TextView as the HeaderView that also says "the list is empty" and make sure to hide the header when you add the real results. I think the first option might be the best best.

1 Comment

I was adding a item to arraylist saying the list is empty. But I felt it was not a clean implementation. Also my row of the list has now got an image too. That is why I posted this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.