How to set an empty ListView wallpaper?

How did you set up such a list of background list. I want to appear when the number of entries is 0

enter image description here

+6
source share
5 answers

ListView has a special method - setEmptyView() . You can find usage examples here or here .

Update: The second link is not available. Here is a quote from the article:

When you arbitrarily set the "empty view" of ListViews, you can scratch your head about why your empty view does not actually appear when the list is empty.
If this happens, then you forgot that you must manually add your empty view to your view hierarchy, because the ListView will not do this for you. Although this is obvious when you think about it, the documentation does not mention this detail, and Googling shows that one person had the problem.

Here is the code with lines that are too easy to forget on 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); 
+14
source

Just set the background image in the parent layout, and then set the color of the ListView to be completely 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> 
+7
source

Read the ListActiviy documentation . You can define a view that will be automatically displayed when the list is empty and has no elements. In the view for the empty list, the identifier android:id/empty .

So no need to play with the background.

+3
source

You can set the selection as background using ListView.setBackgroundDrawable ()

0
source

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

0
source

Source: https://habr.com/ru/post/889860/


All Articles