Java – Android ListView padding for content without moving ListView height

androidjavalistview

I have a ListView with a bunch of items inside it. How do I make the top and bottom items have a top margin of 10dp for top item and bottom margin of 10dp for bottom item? Now I can do this with padding or margin on the ListView but the result is that when you scroll the edge of the ListView is now 10dp from the bottom of the screen. Anyway around this? I have also tried setting margins inside of the getView method of my adapter but I don't see any margin options for AbsListView.LayoutParams. Any help would be great.

Thanks

Best Answer

The trick is to include android:clipToPadding="false" in your view definition, e.g.:

<ListView android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="16dip"
    android:paddingBottom="16dip"
    android:clipToPadding="false" />