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)
Related
I'm using Chris Bane's "deprecated" Pull-to-Refresh library. Everything's working nicely. What I'm able to do:
1.) When there's content in the listview I can do a pull to refresh. I can see the progress dialog spinning and everything.
2.) On first load of the app if there's no data/empty listview, I can show the empty textview and I am able to do the same pull to refresh with the spinning progress bar showing.
But the second time I go to this listview (that's inside a Fragment), with empty data nothing happens. Can't do a pull to refresh -- the progress dialog doesn't show, doesn't even pull the layout. It just shows an empty text (that you can't pull anymore).
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/ptr_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="#19000000"
android:dividerHeight="1dp"
android:fadingEdge="none"
android:fastScrollEnabled="false"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:descendantFocusability="blocksDescendants"
app:ptrDrawable="#drawable/ic_logo_blue"
app:ptrRotateDrawableWhilePulling="false"
app:ptrScrollingWhileRefreshingEnabled="false"
app:ptrHeaderBackground="#EEEEEE"
app:ptrRefreshableViewBackground="#F7F7F7"
android:smoothScrollbar="true" />
<TextView
android:id="#+id/emptyView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:text="Empty list"
android:gravity="center"
android:visibility="gone"/>
I also tried replacing the RelativeLayout with LinearLayout, but the issue still exists.
Additional Info
Not sure if these are relevant:
The Fragment that contains the listview is inside a SlidingTab
The listview reference to the pull to refresh listview has a height of 0.
Here's the code to the Fragment's onCreateView()
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
result = (RelativeLayout)inflater.inflate(R.layout.listview, container, false);
listView = (PullToRefreshListView)result.findViewById(R.id.directoryList);
emptyTextView = (TextView)result.findViewById(R.id.emptyEventsView);
adapter = new ListViewAdapter(getActivity(), R.layout.listview_item, someList);
listView.setAdapter(adapter);
listView.setEmptyView(emptyTextView);
listView.setOnItemClickListener(this);
listView.setOnRefreshListener(this);
listView.post(new Runnable() {
#Override
public void run() {
listView.setRefreshing();
}
});
return result;
}
Sorry for the long post. Any help would be appreciated.
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 );
//...
}
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 have 2 layout xml files: "highlights.xml" and "highlights_cell.xml".
Here is a simplified version of each. I've removed the width/height/etc and just kept the important attributes...
highlights.xml
<LinearLayout>
<uk.co.jasonfry.android.tools.ui.SwipeView android:id="#+id/swipe_view" />
<uk.co.jasonfry.android.tools.ui.PageControl android:id="#+id/page_control" />
</LinearLayout>
highlights_cell.xml
<LinearLayout android:orientation="vertical">
<LinearLayout android:id="#+id/linear_layout1" android:orientation="horizontal">
<ImageView android:id="#+id/logo" />
<LinearLayout android:id="#+id/linear_layout2" android:orientation="vertical">
<TextView android:id="#+id/title" />
<TextView android:id="#+id/subtitle" />
</LinearLayout>
</LinearLayout>
<ScrollView android:id="#+id/scroll_view">
<TextView android:id="#+id/description" />
</ScrollView>
</LinearLayout>
The idea is that I want to add several "highlights_cell" to "highlights" through a loop.
I've thrown together some test code as follows but, as it's not working, I suspect that I'm not adding the cell layouts correctly, or perhaps I shouldn't be using "inflater"...
/** Declare shared variables */
SwipeView mSwipeView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState){
//Initialise layout and variables
super.onCreate(savedInstanceState);
setContentView(R.layout.highlights);
//Setup controls
mSwipeView = (SwipeView) findViewById(R.id.swipe_view);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
//Loop through collection and add views
for(int i=0; i<7;i++)
{
//Create the itemView to use layout xml for each cell
View itemView = inflater.inflate(R.layout.highlights_cell, null);
//Set values within cell
TextView title = (TextView) itemView.findViewById(R.id.title);
title.setText("HELLO WORLD_" + i);
//add the itemView to main view
mSwipeView.addView(itemView);
}
}
Is this the correct way to add layouts dynamically to a parent layout?
Thanks!
It looks good except for a few things.
Because you are adding views to your custom ViewGroup, you will have to be sure that it correctly lays out and displays its children.
Also, when you add a View to a ViewGroup, you specify the LayoutParams that views can have in that ViewGroup.
Some more info about creating a custom ViewGroup
http://about-android.blogspot.com/2010/05/create-dynamic-view-group.html
custom ViewGroup example?
I wanted to create a search view like the one Google uses. For this I created the following XML layout, which basically is a search bar and a button in the upper section of the screen and a ListView at the bottom of it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayoutSearch"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FF394952">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<EditText android:layout_height="wrap_content" android:id="#+id/searchTextBar" android:layout_width="wrap_content" android:layout_weight="1">
<requestFocus></requestFocus>
</EditText>
<Button android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/searchButton" android:text="Buscar"></Button>
</LinearLayout>
<ListView
android:id="#+id/searchResultList"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_weight="1.0" />
</LinearLayout>
And this is the code of the textViewResource that the ArrayAdapter demands on its constructor:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
Now, this is the code of the activity. So far, I just want to display the view with the contents (that's why I'm using a static String array for now).
public class SearchActivity extends Activity{
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_item, COUNTRIES);
ListView lv = (ListView)this.findViewById(R.id.searchResultList);
lv.setAdapter(adapter);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
However, when I run the activity I see the search bar but it doesn't display the ListView.
I've tried changing the extension of SearchActivity to ListActivity, but the program just crashes when I try to start it. I'm also aware of the existence of the Search Interface, but I just want to see this particular method work.
Why it doesn't display the contents of the ListView? Is there a way to fix this?
Thanks in advance
If you are going to use ListActivity you should be aware that ListActivity already has a ListView instance. You need to call its setListAdapter method to set the adapter for its ListView instead of instantiating your own ListView and setting the adapter on it. You can call getListView to get a handle on ListActvity's ListView and then set the click listener on that.
If you want to extend ListActivity then you must have a ListView with id #android:id/list. Change the id of your ListView, that should fix the crash when extending ListActivity.