How to set space between listView Items in Android
@Asahi pretty much hit the nail on the head, but I just wanted to add a bit of XML for anyone maybe floating in here later via google:
<ListView android:id="@+id/MyListView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:divider="@android:color/transparent"
android:dividerHeight="10.0sp"/>
For some reason, values such as "10", "10.0", and "10sp" all are rejected by Android for the dividerHeight
value. It wants a floating point number and a unit, such as "10.0sp". As @Goofyahead notes, you can also use display-independent pixels for this value (ie, "10dp").
Perhaps divider
or dividerHeight
property of the ListView
can solve your problem.
Although the solution by Nik Reiman DOES work, I found it not to be an optimal solution for what I wanted to do. Using the divider to set the margins had the problem that the divider will no longer be visible so you can not use it to show a clear boundary between your items. Also, it does not add more "clickable area" to each item thus if you want to make your items clickable and your items are thin, it will be very hard for anyone to click on an item as the height added by the divider is not part of an item.
Fortunately I found a better solution that allows you to both show dividers and allows you to adjust the height of each item using not margins but padding. Here is an example:
ListView
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
ListItem
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Item"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
You should wrap your ListView
item (say your_listview_item
) in some other layout e.g LinearLayout
and add margin to your_listview_item
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<your_listview_item
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
...
...
/>
</LinearLayout>
This way you can also add space, if needed, on the right and left of the ListView
item.
My solution to add more space but keep the horizontal line was to add divider.xml
in the res/drawable
folder and define line shape inside:
divider.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="1px"
android:color="@color/nice_blue" />
</shape>
then in my list I reference my divider as follows:
<ListView
android:id="@+id/listViewScheduledReminders"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_marginBottom="@dimen/mediumMargin"
android:layout_weight="1"
android:divider="@drawable/divider"
android:dividerHeight="16.0dp"
android:padding="@dimen/smallMargin" >
</ListView>
notice the android:dividerHeight="16.0dp"
by increasing and decreasing this height I am basically adding more padding on top and bottom of the divider line.
I used this page for reference: http://developer.android.com/guide/topics/resources/drawable-resource.html#stroke-element