I am using addHeaderView to add a view item to the top of a ListView. I also have a TextView to display a message saying there are no items in the list.
Here is the layout:
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/list_empty"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
And the Java code:
final ListView listView = getListView();
final View view = getLayoutInflater().inflate(R.layout.list_item_add,
listView, false);
listView.addHeaderView(view, null, true);
When there are items in the ListView then the header is shown but if I delete all items in the list (except the header view) then the header view disappears.
I would like the header view to be visible in the list view whether there are items in the list or not.
Thanks,
Remove the #android:id/empty view from your layout, or override/subclass your adapter to return false from isEmpty()
From my experience (SDK version 10):
Overriding isEmpty() in the adapter makes it work.
Then, it is optional to remove the #android:id/empty view.
Related
I have a simple list view, and I want to add header and footer to this list view without setting an adapter for it.
How do I add a header and footer to a ListView without setting an adapter, if it is possible?
xml code for listView:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"/>
and code for adding header and footer:
ListView listView = (ListView)findViewById(R.id.listView);
Button headerButton = new Button(getApplicationContext());
headerButton.setText("Header");
listView.addHeaderView(headerButton);
Button footerButton = new Button(getApplicationContext());
footerButton.setText("Footer");
listView.addFooterView(footerButton);
You won't be able to achieve that.
The way header and footer layouts work with ListView is the following: Whenever you add a header or footer to your ListView, it will cause your own adapter to be wrapped by a WrapperListAdapter and basically make your header and footer items be treated as list items in their own way(handled by the adapter of course). Thus, your ListView can't really know about these items if it has no adapter.
I have a listview inside of a viewpager. I set an empty view on the listview. If this listview starts empty, when i add something to it then right or left swipes on the listview won't switch between tabs. If the listview starts with something (not empty) when i set the adapter on it, then i can swipe. If i remove the setEmptyView on the listview, it always works whether it starts with data or without it.
How can i set an empty view and still get the listview to swipe between tabs?
This is how i set the empty view:
mListview.setEmptyView(findViewById(R.id.empty_view));
the empty view is a textview directly below the listview in a linearlayout
the empty view xml:
empty_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/empty_list"
android:textColor="#color/lighter_gray"
android:layout_gravity="center"
android:gravity="center"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/no_item"
android:textStyle="italic">
</TextView>
I would suggest removing your empty view from the current layout file, and move it into its own dedicated layout file. Then inflate this new layout in your class when you want to use it as an empty view. The end result would be something like this (not compiled, coding from memory):
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, null);
mListview.setEmptyView(v);
or maybe
final View v = LayoutInflater.from(context).inflate(R.layout.empty_view, mListview, false);
mListview.setEmptyView(v);
I need to make a list view in which I want to have a divider at some position only not after every list item. I am using custom list view.
Is there any solution of this problem?
you can you this xml file in list adapter class like
ItemsAdapter ItemsAdapter = new ItemsAdapter(EnterpriseFertilisersScreen.this,
R.layout.list, Constant.FERTILIZERMANAGERARRAY);
R.layout."below xml file " and user as further white color.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#color/list_bg" >
<TextView
android:id="#+id/post"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="7dp"
android:layout_toRightOf="#+id/bite_image"
android:gravity="center_vertical"
android:textColor="#android:color/white"
android:textSize="20sp" />
</RelativeLayout>
If you have another issues, ask feel free..
You can create Adapter for list, which will be place dividers as elements (via getView).
This is standart android approach
If you have used Custom ListView to show your .you need to make position where you need to show different View from xml by condition .you should have to do this in getView Method.
Either you need to see how this example use divider using CursorAdapter.
Check this https://github.com/cyrilmottier/ListViewTipsAndTricks/blob/master/src/com/cyrilmottier/android/listviewtipsandtricks/SectionedListActivity.java
you can add it to getview Methoid as follow :
public View getView(int position, View convertView, ViewGroup parent) {
if(items.get(position).get("name").startsWith("-")){
View divider = mInflater.inflate(R.layout."yourlayout",null);
return divider; }
also, you must add item names starting with "-" where you want to add a divider.
Hope this helpful
I have a Fragment with a ListView. After setting ListView's emptyView everything works ok, but as soon as my adapter (which is extending BaseAdapter) gets some data not only emptyView but also whole Fragment containing my ListView and few other Views disappear.
If I do not use empty view everything works as it should.
Does my adapter need to implement something special for empty view to work? Or it is the way I set emptyView?
LayoutInflater inflater = getActivity().getLayoutInflater();
View emptyView = inflater.inflate(R.layout.empty_list_view,
(ViewGroup) mListView.getParent());
mListView.setEmptyView(emptyView);
Thank you,
Marcin.
So I figured it out.
I now add empty view directly the place it should be in XML and then setEmptyView of my ListView in Java, and that works like I wanted it to work.
XML:
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/empty_list_view" />
and Java part, setting emptyView:
View emptyView = getActivity().findViewById(R.id.empty_view);
mListView.setEmptyView(emptyView);
Adapter should implement isEmpty():
#Override
public boolean isEmpty()
{
return listItems.size() == 0;
}
I hope it will help someone in the future. ;)
I've created some composie UIs in my android apps and there are some ListView controls -among other controls- inside a view. Because of this, I have used "Activity" as my activity base class.
Now I need to display a simple message like "No Item" when the ListView that is bound to my adapter is empty. I know this is possible when using ListActivity but I'm not sure what's the best approach for this?
You can have an empty view without a ListActivity! The correct method is as follows
First add an 'empty view' to your layout XML below your list
...
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/empty"
android:text="Empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
...
Next override the onContentChanged method of your activity and set the empty view of your list to your empty view:
#Override
public void onContentChanged() {
super.onContentChanged();
View empty = findViewById(R.id.empty);
ListView list = (ListView) findViewById(R.id.list);
list.setEmptyView(empty);
}
That's it! Android will take care of hiding/showing the list and empty view when you update the adapter.
The Magic
Deciding whether the empty view is shown or not is handled by the superclass of ListView, AdapterView. AdapterView registers a DataSetObserver on the set adapter so it is notified whenever the data is changed. This triggers a call to checkFocus in AdapterView which contains the following lines
if (mEmptyView != null) {
updateEmptyStatus((adapter == null) || adapter.isEmpty());
}
and sets the empty view visibility based on whether the adapter is empty or not.
You're looking for the empty view of a ListActivity:
ListActivity
If you're using ListView you can use the method setEmptyView():
setEmptyView
Just combine your ListView with TextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
<TextView
android:id="#+id/list_empty"
android:text="No Item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
Then check the count of items an chanche visibility on ListView accordingly:
ListView lv = (ListView)findViewById(R.id.list);
lv.setVisibility((adapter.isEmpty())?View.GONE:View.VISIBLE);
If you are using Custom Adapter, you can do this in the overridden notifyDataSetChanged method.
You can use Toast Message for this..
Check the Count of the Adapter value by adapter.getCount()
if(Adapter.getCount()!=0){
List.setAdapter(Adapter);
}else{
Toast.makeText(YourActivityName.this, "No Items Available",Toast.LENGTH_SHORT).show();
}
For the layout code by Joseph, you need to edit the #+id/list and #+id/empty to #android:id/*, like:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#android:id/empty"
android:text="Empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
This way, you even don't need to override the onContentChanged() function.
The easiest way to achieve this was using a ListFragment instead of a ListActivity. ListFragment has the following convenience method:
setEmptyText("My no items message...");
Besides, using a ListFragment class has other advantages. For example, the possibility to combine it with the new AppCompat library (which you cannot do with ListActivity because you have to extend from ActionBarActivity).