6

How yo set listview background like this. I want to appear when the number of record 0

enter image description here

Phillip
  • 5,366
  • 10
  • 43
  • 62
david
  • 753
  • 4
  • 14
  • 26

5 Answers5

14

There is special method in ListView - setEmptyView(). You can find examples of using it here or here.

Upd: second link is unavailable now. Here is quote from article:

When you set a ListView’s “empty view” programmatically, you can end up scratching your head as to why your empty view actually doesn’t appear when the list is empty.
If this happens, then what you forgot is that you must manually add your empty view to your view hierarchy, cos ListView won’t do it for you. Although it’s obvious when you think about it, the documentation doesn’t mention this detail, and Googling shows at least one person had the problem.

Here’s the code with the lines that it’s all too easy to forget at numbers 4 and 5…

TextView emptyView = new TextView(context);
emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
emptyView.setText(“This appears when the list is empty”);
emptyView.setVisibility(View.GONE);
((ViewGroup)list.getParent()).addView(emptyView);
list.setEmptyView(emptyView);
Community
  • 1
  • 1
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
7

Just set a background image at the parent layout and then set the color of the ListView to fully transparent:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    style="@style/Main" android:background="@drawable/background">
    <ListView android:cacheColorHint="#00000000" .../>
</LinearLayout>
Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
3

Read the documentation of the ListActiviy. You can define a view which will automatically shown when the list is empty and has not items. The view for the empty list got to have the id android:id/empty.

So no need to play around with the background.

Flo
  • 27,355
  • 15
  • 87
  • 125
0

You can set a drawable as background with ListView.setBackgroundDrawable()

Karl-Bjørnar Øie
  • 5,554
  • 1
  • 24
  • 30
0

You need to check before passing array/arraylist into adapter ,if the length of array/arraylist is 0 then add this image to your main layout.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
Hiren Dabhi
  • 3,693
  • 5
  • 37
  • 59