I would like to display Empty View like "No Items Found" in ListActivity if it is Empty. I have searched more on sites and all suggestions for setEmptyView method of ListView only. Can you please guide for ListActivity?
I have found the solution as below
TextView emptyview = new TextView(this);
emptyview.setText("Empty List");
emptyview.setTextSize(25);
ListView currentlistview = getListView();
if(listadapter.size()==0)
((ViewGroup)currentlistview.getParent()).addView(emptyview);
I have to search more for the solution... ;)
You can use the getListView().setEmptyView(yourTextView); method in ListActivity.
TextView emptyView = new TextView(mContext);
emptyView.setText("Empty List");
emptyView.setTextSize(25);
emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
getListView().setEmptyView(emptyView);
Edit
I just found that Cyril Mottier wrote a blog about this. It seems that it is exactly what you would like to do.
For add an empty view for ListActivity according too source code:
Create a xml layout, for example R.layout.list_activity, with ListView and TextView with identifiers (required!) android:id/list, android:id/empty.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
>
<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="match_parent"
android:id="#android:id/empty"
android:text="#string/empty"
android:gravity="center"
/>
</LinearLayout>
Set this layout:
#Override
protected void onCreate( Bundle savedInstanceState ){
super.onCreate( savedInstanceState );
setContentView( R.layout.list_activity );
//...
}
Related
obviously not a profound question, but I think theres probably an error in my approach.
So I've added the file listview.xml to my project in the layout folder.
it contains this:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
I've also tried:
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
And i my main activity function generally looks like this:
public class ToDoManagerActivity extends ListActivity {
private static final int ADD_TODO_ITEM_REQUEST = 0;
private static final String FILE_NAME = "TodoManagerActivityData.txt";
private static final String TAG = "Lab-UserInterface";
// IDs for menu items
private static final int MENU_DELETE = Menu.FIRST;
private static final int MENU_DUMP = Menu.FIRST + 1;
ToDoListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
// TODO - Inflate footerView for footer_view.xml file
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
// NOTE: You can remove this block once you've implemented the assignment
if (null == footerView) {
return;
}
// TODO - Add footerView to ListView
getListView().addFooterView(footerView);
setListAdapter(mAdapter);
setContentView(R.layout.footer_view);
This keeps breaking with the error:
...java.lang.RunTimeException: content must have a ListView whose id attribute is 'android.R.id.list'
I noticed that if I comment out the setContentView(R.layout.footer_view) bit the error disappears, which is quite confusing as well, because it seems like the error should trigger before there.
This is confusing the hell out of me because as far as I can tell this element exists. Its seems like maybe I'm missing a step to load the ListView? I'm banging my head against the wall here and this seems like something really basic, and being a n00b sucks...So any help is much appreciated!
Cheers!
EDIT:
footer_view.xml contents:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
EDIT2:
Current onCreate after suggested edits:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a new TodoListAdapter for this ListActivity's ListView
mAdapter = new ToDoListAdapter(getApplicationContext());
// Put divider between ToDoItems and FooterView
getListView().setFooterDividersEnabled(true);
setListAdapter(mAdapter);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, getListView(), false);
getListView().addFooterView(footerView);
}
For an Activity that is extending ListActivity, the ListView needs to exist within the xml file that you are using in the activity, in your case that's the footer_view file.
Taken from the Google Dev Site - "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""
To incorporate your footer below your list, try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/footerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/add_new_todo_item_string"
android:textSize="24sp" >
</TextView>
</LinearLayout>
Here, try this in your layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:id="#android:id/list" />
</LinearLayout>
That is exactly what I have in my app.
Then in your onCreate(), just this:
setContentView(R.layout.activity_main);
Try it and let me know if it works. That code should work. From there, we can continue on to isolate the problem.
EDIT:
Man, now I see your problem. Not a big deal here. Let's look at your code (edited to be shorter). The onCreate() method has this:
super.onCreate(savedInstanceState);
mAdapter = new ToDoListAdapter(getApplicationContext());
getListView().setFooterDividersEnabled(true);
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
getListView().addFooterView(footerView);
setListAdapter(mAdapter);
setContentView(R.layout.footer_view);
Do you see R.layout.activity_main anywhere there? There's your issue! Your R.layout.footer_view only contains a textview, it doesn't contain the ListView, and your Activity keeps looking for the ListView that you promised it that you'd have by extending a ListActivity.
Try this - change your onCreate() method to this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new ToDoListAdapter(getApplicationContext());
setListAdapter(mAdapter);
}
Then your activity_main.xml layout file should look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:id="#android:id/list" />
</LinearLayout>
This should work. Let me know.
EDIT2:
Now add this to the end of your onCreate() method:
TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);
getListView().addFooterView(footerView);
Voila! Should work now.
I am trying to make a listview in android but i got some troubles.
This is the code:
public class MainActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] items = {"red", "blue", "green"};
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items));
}
}
And this the xml file:
<?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">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Agregar Tarea"
android:id="#+id/btn" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/list" />
</RelativeLayout>
The logcat says that there is a NullPointerException in line 21, this line:
ListView listView = (ListView) findViewById(R.id.list);
But I dont know why :(
I want to do the list view this way becouse if I use listactivity the header/footers is not fixed.
Add listview id in the xml layout.
Because of id null pointer exception is thrown.
android:id="#+id/list"
instead of
android:layout_below="#+id/list"
and also change above line like this
android:layout_below="#+id/btn"
Try change this
android:layout_below="#+id/list"
to this:
android:id="#+id/list"
Your listview id doesn't exists
android:id="#+id/list"
android:layout_below="#+id/btn"
You have not added property
android:id=#"+id/list"
I have a listview in my code and I want to set adapter on it.
But the issue is, even after initializing the listview, it is still null resulting into nullPointerException. (I checked it by logging and debugging)
I'm not able to access any view from that xml.
What am I missing? Any help appreciated.
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvToday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFF"
android:dividerHeight="2dp" />
<ListView
android:id="#+id/lvTomorrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/lvToday"
android:divider="#FFF"
android:dividerHeight="2dp" />
</RelativeLayout>
</ScrollView>
Activity file
public class DashboardActivity extends Activity {
ListView lvToday, lvTomorrow;
TextView lblCall;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
init();
}
private void init() {
lvToday = (ListView) findViewById(R.id.lvToday);
lvTomorrow = (ListView) findViewById(R.id.lvTomorrow);
TodayAppAdapter adapter = new TodayAppAdapter(DashboardActivity.this,
DashboardActivity.this);
// This line is giving NULL
lvToday.setAdapter(adapter);
TomorrowAppAdapter adapter1 = new TomorrowAppAdapter(
DashboardActivity.this, DashboardActivity.this);
// This line is giving NULL
lvTomorrow.setAdapter(adapter1);
}
}
1) test eclipse menu: Project -> Clean...
2) if you have more than one version for your xml layout (example layout-large, layout-xlarge,...), check if all of them have your view.
You are missing a TextView in your layout
Put a String []
along with name of your Adapter in your
OnDraw()
Method
I have this code:
public class MyActivity extends ListActivity implements OnClickListener {
private ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setAdapter();
this.bindButtons();
}
private void setAdapter() {
setContentView(R.layout.siteactivity);
adapter=new ArrayAdapter<String>(this,
R.layout.siteitem,
listItems);
setListAdapter(adapter);
}
private void bindButtons() {
findViewById(R.id.buttonPrevious).setOnClickListener(this);
findViewById(R.id.buttonNext).setOnClickListener(this);
}
// ...
}
with this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/siteActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/navigation">
<Button
android:text="<="
android:textSize="12dp"
android:id="#+id/buttonPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="=>"
android:textSize="12dp"
android:id="#+id/buttonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/navigation"
android:layout_above="#+id/transport_selection"
/>
</RelativeLayout>
Note the android:id="#android:id/list" of the ListView. If I replace it by android:id="#+id/list", my activity force closes, because my "content must have a ListView whose id attribute is 'android.R.id.list'", but this is the expected behaviour, I think.
Now, I want to add a context menu to the items of the ListView. I tryed registerForContextMenu(findViewById(R.id.list));, but it doesn't work, because of the android:id.
Then, how can I add a context menu?
Regards,
As you are using ListActivity, you can use the following code to get list view in code:
ListView myListView=getListView ();
Use android.R.id.list instead of R.id.list.
(And by the way, the error message you wrote here turns out to have the answer)
Basically, anything defined in XML as #android:whatever is the same as android.R.whatever in the Java code. Anything that you defined in XML, which will look like #whatever, is the same as R.whatever in Java code.
Final edit: If you're using ListActivity, you are required to have a ListView with the id #android:id/list. Thus, unless you want to add another ListView to your activity, you shouldn't need #+id/list (or R.id.list).
You need to create an AlertDialog and call the dialog to show inside the listview's onItemLongClick() event. Click here for an example.
I am developing an application,In my application,I am using Listview for displaying data using dom parsing,I want to footer in listview,when i click footer additional more data add to list view,I attached image,i would like that design and process,please refer image1 and imgae2.I mention footer in red rectangle
Fig1-Footer like "More News"
Fig2-Add additional 10 record added in listview
Create a footer view layout consisting of text that you want to set as footer and then try
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
ListView.addFooterView(footerView);
Layout for footer could be something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="#string/footer_text_1"
android:id="#+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip" />
</LinearLayout>
</LinearLayout>
The activity class could be:
public class MyListActivty extends ListActivity {
private Context context = null;
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)findViewById(android.R.id.list);
//code to set adapter to populate list
View footerView = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
}
}
Answers here are a bit outdated. Though the code remains the same there are some changes in the behavior.
public class MyListActivity extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
TextView footerView = (TextView) ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_view, null, false);
getListView().addFooterView(footerView);
setListAdapter(new ArrayAdapter<String>(this, getResources().getStringArray(R.array.news)));
}
}
Info about addFooterView() method
Add a fixed view to appear at the bottom of the list. If addFooterView() is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.
Most of the answers above stress very important point -
addFooterView() must be called before calling setAdapter().This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.
From Kitkat this has changed.
Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.
Documentation
I know this is a very old question, but I googled my way here and found the answer provided not 100% satisfying, because as gcl1 mentioned - this way the footer is not really a footer to the screen - it's just an "add-on" to the list.
Bottom line - for others who may google their way here - I found the following suggestion here: Fixed and always visible footer below ListFragment
Try doing as follows, where the emphasis is on the button (or any footer element) listed first in the XML - and then the list is added as "layout_above":
<RelativeLayout>
<Button android:id="#+id/footer" android:layout_alignParentBottom="true"/>
<ListView android:id="#android:id/list" **android:layout_above**="#id/footer"> <!-- the list -->
</RelativeLayout>
If the ListView is a child of the ListActivity:
getListView().addFooterView(
getLayoutInflater().inflate(R.layout.footer_view, null)
);
(inside onCreate())
The activity in which you want to add listview footer and i have also generate an event on listview footer click.
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list_of_f = (ListView) findViewById(R.id.list_of_f);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.web_view, null); // i have open a webview on the listview footer
RelativeLayout layoutFooter = (RelativeLayout) view.findViewById(R.id.layoutFooter);
list_of_f.addFooterView(view);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg" >
<ImageView
android:id="#+id/dept_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dept_nav" />
<ListView
android:id="#+id/list_of_f"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dept_nav"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:divider="#null"
android:dividerHeight="0dp"
android:listSelector="#android:color/transparent" >
</ListView>
</RelativeLayout>
In this Question, best answer not work for me. After that i found this method to show listview footer,
LayoutInflater inflater = getLayoutInflater();
ViewGroup footerView = (ViewGroup)inflater.inflate(R.layout.footer_layout,listView,false);
listView.addFooterView(footerView, null, false);
And create new layout call footer_layout
<?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">
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Done"
android:textStyle="italic"
android:background="#d6cf55"
android:padding="10dp"/>
</LinearLayout>
If not work refer this article hear
you can use a stackLayout, inside of this layout you can put a list a frame, for example:
<StackLayout VerticalOptions="FillAndExpand">
<ListView ItemsSource="{Binding YourList}"
CachingStrategy="RecycleElement"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Image, Mode=TwoWay}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Frame BackgroundColor="AliceBlue" HorizontalOptions="FillAndExpand">
<Button Text="More"></Button>
</Frame>
</StackLayout>
this is the result:
// adding a footer to list as Log Out
val view: View =
(getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(
R.layout.logout_menu, null, false
)
// footer onClick
view.setOnClickListener {
doLogout()
binding.drawerLayout.close()
}
expandableListView.addFooterView(view)