Set emptyView of android listActivity - android

i have created a My custom listActivty by extending ListActivity like this.
public class MainList extends ListActivity {
ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listView = getListView();
setContentView(R.layout.list_main);
}
}
And declaring its xml like this
<?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">
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:text="My Text"
/>
</LinearLayout>
i am using CursorAdapter as listActivity adapter.Its showing perfectly with MainList is empty.
My problem is that i want to show more than a textview when MainList is empty. Basically i need custom view(imageview,button etc) to be shown when list is empty.How should i do this.
Thanks

For the listview there is one default method to set empty view.
setEmptyView(view)
Try This.... In the place of view pass your custom view.
If you are not getting then tell me. I will post an example for this.

You may want to count the items in your CursorAdapter. If 0 is returned, hide/show parts from your XML? That's how I done similar, but they may be better ways.

Try using the android:id/empty for your custom view. I think you can apply the id to any view, not just a TextView

Related

How to dynamically change text of empty view in listfragment

I have red many solutions regarding this but none of them solved my problem.
What i want to do is, when my list view is empty its ok if the default empty text view shows up (which is declared in xml) but what if i want to change its text at some point in an activity that extends listfragment according to my need and make it visible....
Recently i tried this:
public void setEmptyText(CharSequence emptyText) {
ListView mListView = (ListView) NotificationsView.findViewById(android.R.id.list);
View emptyView = mListView.getEmptyView();
((TextView) emptyView).setText(emptyText);
}
It doesn't give any error but it is not solving the problem too. Any Help would be appreciated thanks
EDIT:
here is my layout of listfragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/fragment_bg"
android:divider="#null"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_style" >
</ListView>
<TextView
android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/fragment_bg"
android:text="No data" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
If you are using ListFragment all you need to do is call the method
setEmptyText("Your empty text"):
The system will recognize the view label with the android:id="#id/android:empty" id and will change the text for you. No need to reference it or do any cast, it's all built in the ListFragment
By default the listview or adapter (don't remember which one) makes it invisible. Call emptyView.setVisibility(View.VISIBLE); to make it visible.
This is how to change the value of a text view:
TextView tv = (TextView) findViewById(R.id.empty);
tv.setText('yourText');

Adding LinearLayout to ListView using an adapter

I am trying to create a ListView where each entry in the list consists of a LinearLayout. I have an ArrayList that I have defined here:
ArrayList<LinearLayout> menuList;.
Later in my code, I define
LinearLayout dailyMenuLayout = new LinearLayout(ReturnMenus.this);, and every time I complete a layout to be added to the ListView, I use menuList.add(DailyMenuList)
The adapter that I have been trying to use is as follows -- but it crashes the app every time that it is triggered.
ListView myListView = (ListView)findViewById(android.R.id.list);
ArrayAdapter<LinearLayout> adapter = new ArrayAdapter<LinearLayout>(ReturnMenus.this, R.id.linear_layout_item, menuList);
myListView.setAdapter(adapter);
And this is the XML for the single row in the ListView:
<?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"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linear_layout_item"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
Can somebody show me how to accomplish this? Thanks.
Use something like following layout for the row layout in your list rather than you current layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
.
.
.
.
.
</LinearLayout>
Here if you do not know exactly how many textView that you are going to use, use maximum number of TextView that using in your application. Then in your adapter class inside getView() you can get those TextView. If some rows not going to use all of these textViews then set them invisible as like this:
TextView txtView3 = (TextView)findViewById(R.id.text3);
txtView3.setVisibility(View.GONE)
Following tutorial helps you to create the listView.
http://www.vogella.com/tutorials/AndroidListView/article.html
You should bind data to adapter,such as strings,not the layout.
To see how to use ListView and Adapter,please check the demo project in your-android-sdk-dir\samples\android-XX\ApiDemos.

Android: set empty view to a list view

<TextView
android:id="#android:id/empty"
style="#style/FacebookFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/no_result_found"
android:textColor="#color/white" />
this is the xml code for empty view. I am setting empty view as follows:
list.setAdapter(adapter);
list.setEmptyView(findViewById(android.R.id.empty));
I have the items in the listview even then also before listview is populated the empty view is shown.
I don't want that empty view to be shown when listview contains some item.
Please help.
if you are using ListActivity then no need to set the empty view manually. Remove list.setEmptyView(findViewById(android.R.id.empty)); from your code.
Change your layout like below then it's automatically add EmptyView if list has no item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
You have to set the empty view before setting the adapter, if your MainActivity is not extending ListActivity.
Try this
list.setEmptyView(findViewById(android.R.id.empty));
list.setAdapter(adapter);
Sorry for the late answer!
Alternate method: Make your activity extend from ListActivity. And add a textview to your list layout with id android.R.id.empty
Try this
View view = getLayoutInflater() .inflate(<resource id of the empty view>, null);
ViewGroup viewGroup= ( ViewGroup)list.getParent();
viewGroup.addView(view);
list.setEmptyView(view);
list.setAdapter(adapter);
There are some correct answers, however, I think this is the best approach I've found:
View emptyView = getLayoutInflater().inflate(R.layout.empty_list_cart, null);
addContentView(emptyView, listView.getLayoutParams()); // You're using the same params as listView
listView.setEmptyView(emptyView);
listView.setAdapter(adapter);
Whatever #dhawalSodhaParmar has explained is correct. But there is a confusion when you look at the way the answer is put down.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SampleActivity">
<ListView
android:id="#+id/list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:divider="#null"
android:dividerHeight="0dp" />
<!-- Empty view is only visible when the list has no items. -->
<TextView
android:id="#+id/empty_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="NO DOCUMENTS AVAILABLE!"
android:textColor="#525252"
android:textAppearance="?android:textAppearanceMedium"
android:visibility="gone"/>
</RelativeLayout>
Suppose if you already have a list view and adapter backing the same with data. And assuming everything is working fine, whoever wants to implement this empty list view screen has to start like below:
Step 1:
Modify the existing Activity XML file which has the list view with the one I have used here. You can use either a Linear Layout or Relative Layout, but preferably Relative because of the flexibility. And a textView like I have written.
Step 2:
Go to your Activity Java file, and after your setContentView
ListView emptyListView = (ListView) findViewById(R.id.list);
// set your adapter here
// set your click listener here
// or whatever else
emptyListView.setEmptyView(findViewById(R.id.empty_text_view));
That's it, now run and you are set!
// first of all create a global variable of Text view
// which is displayed when the list is empty
private TextView mEmptyStateTextView;
//then in onCreate(Bundle savedInstanceState)
/Setting empty text view
mEmptyStateTextView=(TextView) findViewById(R.id.emptyview);
view.setEmptyView(mEmptyStateTextView);
//then in onLoadfinished method
mEmptyStateTextView.setText(R.string.no_earthquakes);

Listview in layout Android

I have a string-array, that i need to display in a layout.
The code currently shows the list, but the list isn't in the layout, its make a new "screen" where the only thing you can see is the list.
Here's the code that i just can't get to show the list in the layouts listview.
import android.app.ListActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class idchart extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.idchart);
Resources res = getResources();
// To get the string array associated with particular resource ID.
String[] ids = res.getStringArray(R.array.idchart_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.test_list_item,ids);
setListAdapter(adapter);
}
}
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_main"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/logo" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<RelativeLayout
android:id="#+id/relative"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
</LinearLayout>
I haven't been able to find anything that helped :( so i hope you will be able to.
ListView listView=(ListView)findViewById(R.id.lv);
//rest of code
listView.setAdapter(adapter);
And you can avoid extending ListActivity.
If you are using ListActivity then
ListView listView=getListView();
Your idchart class should be a subclass of a regular Activity instead of a ListActivity which are for only displaying lists.
Update
Replace
public class idchart extends ListActivity
with
public class idchart extends Activity
If you extend ListActivity, your ListView must have a specific id:
android:id="#id/android:list"
From the doc:
ListActivity has a default layout that consists of a single, full-screen list in the
center of the screen. However, if you desire, you can customize the screen layout by
setting your own view layout with setContentView() in onCreate(). To do this, your own
view MUST contain a ListView object with the id "#android:id/list"

How can I use a listview without extending ListActivity in my class?

I have an application with various tabs (on a tabhost). Each tab is an activity that extends activity and has some textfields and things on it.
Now, I need that my tabs have inside a listview, but in the example from Android developer guide says that you have to extend ListActivity and not Activity.
Basically, I need to merge these two tutorials:
List View
Layouts
How can I use a listview without extending listactivity on my class?
My XML file:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"/>
</LinearLayout>
</TabHost>
It's almost the same. Basing myself on ListView's tutorial.
Instead of doing:
setListAdapter();
do the following:
Add a <ListView> in your layout
create a field var in your Activity private ListView mListView;
on the onCreate() method do this:
mListView = (ListView) findViewById(R.id.your_list_view_id);
mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
I don't remember if ListActivity provides something more.
Your TabHost can contain a ListActivity as well, since it inherits from Activity.
But, in case you want learn how to add a listview in an activity any way, follow these instructions. It's simple enough, Make an Activity, Add a Listview in your XML.
Use findViewById() to get your ListView.

Categories

Resources