4

I have a requirement for my android application that a button is at the bottom end of the list. That is done on purpose so users have to scroll trough the entire list (and possibly see more answers). Therefore I added the button as a listview footer.

This all works fine but in some cases the list is too short (only 3-4 items in it) so the button will end up about midway of the screen. What I would like is that the button is at the end of the listview (so if enough items in the list, the user can only see the button when he scrolls down) but when the listview only contains a few items the button will fix itself to the bottom of the screen.

To give the idea I want:

onCreate() {
   if (button.getyposition() > screen.getyposition()) { // example methodnames!
      // Button at the bottom of the listview (many items)
   }
   else {
      // Button at the bottom of the screen (few items in listview)
   }
}

The point is, I don't know if it's possible and where to start. Could anyone give me some directions where to look or even better a piece of sample code? Thanks in advance!

EDIT:

I added a picture to clarify my problem. I hope this makes more sense!

Picture showing the button halfway on the screen http://niles11.dyndns.org/example.png

As you can see, it looks quite strange with the button halfway on the screen. I want the button to be at the bottom of the screen BUT (!) when the listview contains more items I want it to appear at the bottom of the listview (so at first, be off screen)

Hope this picture helps!

Niles11
  • 553
  • 1
  • 6
  • 17
  • have you solved it with any of the answer below then please mark as answer which you have used. if not whats the status? – Siddhesh Jun 08 '12 at 08:06
  • @Siddhesh I edited my post to clarify my problem a bit more. I hope it helps. – Niles11 Jun 08 '12 at 11:50

3 Answers3

0

I understand you Issue. I have an ideal : you should inflate last item(position = array.size) as a button instead of normal view item.

Tai Tran
  • 1,406
  • 3
  • 15
  • 27
0

if i am not wrong you want your listview on the whole screen with a button below. make listview's height match_parent and android:layout_margingBottom="somedp" and for button marginTop="-somedp" somedp shoulb same on both places...

Siddhesh
  • 1,370
  • 11
  • 28
0

Set the ListView height to fill_parent and add a Button as the footer using addFooterView()

If you have the requirement that you only show the Button when the List is scrolled to the end, then you can look at this post and use the logic to show/hide the footer view (ie, set View.GONE on the footer to hide it, and View.VISIBLE to show it)

stuckless
  • 6,515
  • 2
  • 19
  • 27
  • 1
    The ListView's height is already set to fill_parent and the buttonlayout is added as a footer view (with addFooterView()). However, addFooterView will add that view just under the last item. So if, say, 10 or more items are in the list everything is OK (the button will at first be off screen). But when only 3 or 4 items are in the list, the button will appear directly under the last item in the listview and so the button appears halfway on the screen. But that looks ugly, so in case the listview contains not enough items the button needs to attach itself to the buttom of the screen. – Niles11 Jun 08 '12 at 08:07
  • gotcha -- nice screenshot. Here's what I'd try... Use a `RelativeLayout` for the main view and add the `ButtonBar` using `alignParentBottom`. Then add the `ListView` with `AlignParentTop` and `layoutAbove` bottom `ButtonBar`. This will force the buttons at the bottom of the screen, and the list view should always be above it. – stuckless Jun 08 '12 at 13:08
  • 1
    Yes but that would mean the button is always at the bottom, even when the listview contains many items, isn't it? That's my problem. In some cases the list actually does contain many items and in that case the button should be at the very end of the list, not the bottom of the screen. I feel the only way to do that is increase the top padding of the button-layout (there already is a padding as you can see). But I would need to calculate that depending on the nr of items in the listview. And I don't know how to do that. Any other solution is welcome ofcourse! – Niles11 Jun 08 '12 at 20:06
  • Yes the button would always be there, but, you could conditionally show/hide the footer depending on the size of the ListView, and the position of the ListView. This post might shed on light on how to do that... http://stackoverflow.com/questions/6358428/implementation-of-onscrolllistener-to-detect-the-end-of-scrolling-in-a-listview – stuckless Jun 09 '12 at 00:25