Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView : default text when no items

I've a ListView that can have 0 custom items inside (like "My Downloads").

Is there anyway to show a default text "No download yet" ?

Thanks !

EDIT : here is my solution,

TextView emptyView = new TextView(getApplicationContext()); emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); emptyView.setTextColor(R.color.black); emptyView.setText(R.string.no_purchased_item); emptyView.setTextSize(20); emptyView.setVisibility(View.GONE); emptyView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);  ((ViewGroup)getListView().getParent()).addView(emptyView); getListView().setEmptyView(emptyView); 

alt text

like image 733
William Remacle Avatar asked Nov 03 '10 15:11

William Remacle


2 Answers

public void setEmptyView (View emptyView)  Since: API Level 1 Sets the view to show if the adapter is empty 

Called on your ListView instance.

E.g. mListView.setEmptyView(someView);

You can build view on the fly, or inflate one from the xml.

like image 54
Alex Orlov Avatar answered Oct 16 '22 17:10

Alex Orlov


Unless I misread your question, you can also just specify any view element with an ID of "@android:id/empty" in your layout and it will be taken care automatically for you. For example:

<ListView android:id="@android:id/list" android:layout_width="fill_parent"     android:layout_height="fill_parent" android:visibility="visible"/>  <LinearLayout android:id="@android:id/empty"     android:layout_width="fill_parent" android:layout_height="fill_parent"     android:gravity="center">      <TextView android:text="@string/no_messages"         android:layout_width="wrap_content" android:layout_height="wrap_content"         style="@style/textBoldLight">     </TextView>  </LinearLayout> 
like image 40
DustinB Avatar answered Oct 16 '22 15:10

DustinB