Naskov
Naskov

Reputation: 4179

Button as last item in ListView Android

I need to place a Button to be the last item in the ListView. For example you are scrolling the list view and when you are reaching the end there is a Button, you are clicking it and the list is loading more items and the button is still at the end of that list.

Upvotes: 3

Views: 3336

Answers (3)

Willi Mentzel
Willi Mentzel

Reputation: 29844

You can handle that in the getView() method of the Adapter (like user200798) suggested. This way you have a "scrollable button". Here a piece of code from one of my apps (slighly changed):

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    // inflate the layout
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

    if(position == logEntries.size() - 1) // last element is a Button
    {
        return inflater.inflate(R.layout.list_button, parent, false);
    }
    else
    {
        convertView = inflater.inflate(R.layout.default_list_element, parent, false);
    }

    // manipulate convertView in any way...

    return convertView;
}

Upvotes: 1

user2007998
user2007998

Reputation: 11

I think this way, better you add button as last item of the ListView.
How to do this: Inside your getView() method you will get the 'position', just put an 'if' statement to check whether position is equal to the size of the list then add the button, else add your actual list item which you have inflated from a layout.

Upvotes: 1

Leonidos
Leonidos

Reputation: 10518

You can add any view to the bottom of a ListView. There is API to do this. Here is the answer.

Also I suggest you to look throught endless list view's. Here is example of what you want.

Upvotes: 2

Related Questions