I have a ListView with an empty list catch in the XML and it works fine. How to toggle ListView Empty Text please ?
This is my XML code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/emptyList" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I'm not using a ListActivity. It s possible to do that please ?
You just need to make use of setEmptyView(View v):
person_ListView.setEmptyView(root.findViewById(R.id.empty));
offtopic: ListActivity is using this method also, but with android.R.id.empty.
Im having trouble with implementing list view inside a fragment.
the xml code is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/jo_logo" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
simple ImageView and afterwards the list view.
I've already tried to implement:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(),
android.R.layout.simple_list_item_1, viewList);
setListAdapter(adapter);
where viewList is View[], on the onCreateView function.
seems that it's not working that way inside fragment.
thing is i need that the first view will be ImageView and second one is ListView, and all inside the Fragment.
please help, thank in advance, udi
In your layout xml change android:id="#+id/listView1" to android:id="#android:id/list" otherwise the ListActivity / ListFragment won't find your ListView.
I have a layout with this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/fundo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/consoleListaSimples"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/listaSimples"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
and for which item from the list a have this xml:
<?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="fill_parent" android:background="#drawable/bk_principal_small">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imagemStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/green"/>
<TextView
android:id="#+id/texto1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/padrao" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
and to create the itens I create a SimpleAdapter that I insert the data using the method ListAdapterCreater
this.adapter = new ListAdapterCreater(list.getContext(),itensList,
R.layout.list_simple_item,new String[] {"text","image"},
new int[]{R.id.texto1,R.id.imagemStatus});
to set the text works pretty, but doesn`t work for the image.
what have to be the value of image? or what should I change to works?
Refer the below links which have sample code snippets.
LINK1 it has one textview and one image view in each list item.
LINK2 it has two textview in each list item.
LINK3 it has two textview and one image view in each list item.
I believe you'll have to create new Adapter that extends BaseAdapter.
Here's a few tutorials for you:
http://blog.sptechnolab.com/2011/02/01/android/android-custom-listview-items-and-adapters/
http://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/
If you look closely on those tutorials, the trick lies in how getView() work.
By using the following code.
BuyActivity.java
public class BuyActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction_list_view);
OrderAdapter orderAdapter = new OrderAdapter(this,
R.layout.custom_row_view, new ArrayList<Object>());
setListAdapter(orderAdapter);
orderAdapter.add(new Object());
orderAdapter.add(new Object());
}
}
transaction_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data"
/>
</LinearLayout>
custom_row_view.xml
Please refer to custom_row_view.xml
I was able to get the following outcome.
Besides list, I would also like to have a static label at the bottom of ListActivity.
I try
transaction_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="THIS IS TOTAL STATIC LABEL"
/>
</LinearLayout>
But I am getting
What my expectation is
Is there anything I had missed out?
You have indicated that your ListView is supposed to fill_parent in the vertical orientation, leaving no room for your other widgets. Your weight is being ignored. You probably want to have your android:layout_height be 0dip instead.
Try setting the height of the static textview to wrap_content.
Well you could try Relative Layout to just set the static label anchored to the bottom of the screen. If that's not what you meant, I would try to help you further on. Good luck
For some reason the empty view, a TextView in this case, always appears even when the ListView is not empty. I thought the ListView would automatically detect when to show the empty view.
<RelativeLayout android:id="#+id/LinearLayoutAR"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="#+id/ARListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></ListView>
<ProgressBar android:id="#+id/arProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"></ProgressBar>
<!-- Here is the view to show if the list is emtpy -->
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />
</RelativeLayout>
How can I hook up the empty view properly?
When you extend FragmentActivity or Activity and not ListActivity, you'll want to take a look at:
ListView.setEmptyView()
It should be like this:
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No Results" />
Note the id attribute.
As appsthatmatter says, in the layout something like:
<ListView android:id="#+id/listView" ... />
<TextView android:id="#+id/emptyElement" ... />
and in the linked Activity:
this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.emptyElement));
Does also work with a GridView...
I tried all the above solutions.I came up solving the issue.Here I am posting the full solution.
The xml file:
<RelativeLayout
android:id="#+id/header_main_page_clist1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:paddingBottom="10dp"
android:background="#ffffff" >
<ListView
android:id="#+id/lv_msglist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#color/divider_color"
android:dividerHeight="1dp" />
<TextView
android:id="#+id/emptyElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO MESSAGES AVAILABLE!"
android:textColor="#525252"
android:textSize="19.0sp"
android:visibility="gone" />
</RelativeLayout>
The textView ("#+id/emptyElement") is the placeholder for the empty listview.
Here is the code for java page:
lvmessage=(ListView)findViewById(R.id.lv_msglist);
lvmessage.setAdapter(adapter);
lvmessage.setEmptyView(findViewById(R.id.emptyElement));
Remember to place the emptyView after binding the adapter to listview.Mine was not working for first time and after I moved the setEmptyView after the setAdapter it is now working.
Output:
I highly recommend you to use ViewStubs like this
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ViewStub
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout="#layout/empty" />
</FrameLayout>
See the full example from Cyril Mottier
<ListView android:id="#+id/listView" ... />
<TextView android:id="#+id/empty" ... />
and in the linked Activity:
this.listView = (ListView) findViewById(R.id.listView);
this.listView.setEmptyView(findViewById(R.id.empty));
This works clearly with FragmentActivity if you are using the support library. Tested this by building for API 17 i.e. 4.2.2 image.
Activity code, its important to extend ListActivity.
package com.example.mylistactivity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.example.mylistactivity.R;
// It's important to extend ListActivity rather than Activity
public class MyListActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
// shows list view
String[] values = new String[] { "foo", "bar" };
// shows empty view
values = new String[] { };
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
android.R.id.text1,
values));
}
}
Layout xml, the id in both views are important.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- the android:id is important -->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- the android:id is important -->
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="i am empty"/>
</LinearLayout>
Just to add that you don't really need to create new IDs, something like the following will work.
In the layout:
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/empty"
android:text="Empty"/>
Then in the activity:
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setEmptyView(findViewById(android.R.id.empty));
I had this problem. I had to make my class extend ListActivity rather than Activity, and rename my list in the XML to android:id="#android:id/list"
A programmatically solution will be:
TextView textView = new TextView(context);
textView.setId(android.R.id.empty);
textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setText("No result found");
listView.setEmptyView(textView);
First check the list contains some values:
if (list.isEmpty()) {
listview.setVisibility(View.GONE);
}
If it is then OK, otherwise use:
else {
listview.setVisibility(View.VISIBLE);
}