87

I need to add to add ListView with complicated items background: different for even/odd and rounded corners at the top and bottom. It looks like this:

ListView top

I have implemented all this stuff via level-list, but there is one more thing I want to do. Now the bottom item is near the bottom of the screen. It is better to add some space.

ListView bottom looks not good

I don't want to add bottom margin to ListView, I need margin only for last item.

The ways I see to do this:

Footer

A kind of hack – add footer with empty TextView to ListView. But footers are quite unstable things, they usually disappear after notifyDataSetChanged and there is no way to get them back

Image with transparent pixels

I asked designer to add transparent pixels to bottom background resource. Unfortunately, in this case vertical centering is completely broken. For example, there is 9patch like this:

9patch

And layout like this:

<?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"
        >
    <!-- View with background with transparent pixels on bottom -->
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
                  android:id="@+id/item"
                  android:background="@drawable/some_bgr"
                  android:padding="10dp"
            >
        <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"
                  android:text="Title"
                  android:layout_gravity="center"
                  android:textSize="18sp"
                />
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                  android:text="Detail"
                  android:layout_gravity="center"
                  android:textSize="18sp"
                />
    </LinearLayout>

    <!-- Just for marking place took by view -->
    <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
                 android:layout_below="@id/item"
                 android:background="#88ff55"
            />
</RelativeLayout>

The result:

Result

As you see, centering is not working. Unfortunately. (BTW, if specify this 9patch as background for TextView, centering works good. If you know any article, explaining this, please let me know.)

Add bottom margin to last item in Adapter implementation

That should work, but for unknown reason I still can't get it work. I don't like this way, because I don't like to modify dimensions in code.

So

There is already imaginary way – construct some XML drawable with particular bitmap and margin. According to drawables concept it should be possible, but I can't find implementation. May be somebody knows?

Any other ideas?

darja
  • 4,985
  • 9
  • 33
  • 47
  • you just want to add a margin after the last item? – Goofy Jul 29 '13 at 09:35
  • Yes. The question sounds exactly like this – darja Jul 29 '13 at 09:38
  • I would actually consider using the footer approach. I've never seen them disappear by themselves, especially not after a call to `notifyDataSetChanged()`. You may be doing something else wrong if that's happening. A footer is managed by the `ListView` and has little to do with an adapter's actual dataset. – MH. Jul 29 '13 at 19:13
  • Best way is [http://stackoverflow.com/questions/6288167/add-margin-above-top-listview-item-and-below-last-in-android][1] [1]: http://stackoverflow.com/questions/6288167/add-margin-above-top-listview-item-and-below-last-in-android it really helped me – Bunyod Sep 25 '14 at 10:09
  • Can you please update the accepted answer here? Thanks – clocksmith Apr 13 '16 at 22:03

7 Answers7

294

In your ListView, set a paddingBottom and clipToPadding="false".

  <ListView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="8dp"
      android:clipToPadding="false"
      android:scrollbarStyle="outsideOverlay"/>
  • This also works for RecyclerView.

  • Only use android:scrollbarStyle="outsideOverlay" if you want the scroll bar to not overflow into the padded area.

clocksmith
  • 6,226
  • 3
  • 32
  • 45
8

add an empty footer in your list like this:

TextView empty = new TextView(this);
empty.setHeight(150);
listview.addFooterView(empty);
Tobias
  • 7,282
  • 6
  • 63
  • 85
fullmoon
  • 8,030
  • 5
  • 43
  • 58
4

you can also do it from code if you want, for example here I react to to EditText different situations:

   if(s.toString().length()>0)
   {
      contacts_lv.setClipToPadding(false);
      contacts_lv.setPadding(0,0,0,270*screenDensity);
   }
   else
   {
      contacts_lv.setClipToPadding(true);
      contacts_lv.setPadding(0,0,0,0);
   }
Gal Rom
  • 6,221
  • 3
  • 41
  • 33
2

Clocksmith's answer is the best and pretty clever. You can also create an empty footer view.

SIr Codealot
  • 5,331
  • 9
  • 33
  • 45
1

Add these two lines in your listView XML code:

android:transcriptMode="alwaysScroll"  
android:stackFromBottom="true"
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

Another solution might be that you make a mock view with certain height.

In your adapter in getViewCount return 2.

In getCount return yourData.size+1.

In getViewType check if the element is last element return 2;

Use this type in getView to populate the mockview.

hasn
  • 749
  • 6
  • 21
-2

I guess you want to add margin only to last item:

So you can do in this manner, in your getview method the index of the list item and check if its the last item, then progrmatically add margin to the view.

Goofy
  • 6,098
  • 17
  • 90
  • 156