How to set space between listView Items in Android
Asked Answered
H

19

390

I tried to use marginBottom on the listView to make space between listView Item, but still the items are attached together.

Is it even possible? If yes, is there a specific way to do it?

My code is below

<LinearLayout
android:id="@+id/alarm_occurences"
android:layout_width="fill_parent" 
android:orientation="vertical"
android:layout_height="fill_parent"
android:background="#EEEEFF"
xmlns:android="http://schemas.android.com/apk/res/android">

<ListView
android:id="@+id/occurences"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

My custom List item:

<com.android.alarm.listItems.AlarmListItem
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:background="@drawable/alarm_item_background"
android:layout_marginBottom="10dp"    
>
<CheckedTextView     
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:textSize="20sp"
    android:textStyle="bold"
    android:typeface="serif"
    android:padding="10dp"

/>

</com.android.alarm.listItems.AlarmListItem>

How can I make spacing between list items in this case?

Hydromagnetics answered 13/2, 2011 at 13:10 Comment(0)
G
867

@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").

Guglielmo answered 15/3, 2011 at 9:38 Comment(7)
This doesn't help if you also want to show a divider without stretching itMultiangular
FYI - can be done through code - getListView().setDividerHeight(10)Ev
or android:divider="@null"Muoimuon
@Multiangular I'd have tried a 9-patch if I needed to some something like that.Munday
@Multiangular check my answer. It adds padding but doesn't stretch the divider.Munday
Cannot see the divide line if use this approach.Kirkpatrick
You should almost certainly not be using sp as the height of a divider, unless you're showing text as part of the divider. 10dp (or a reference to a dimen) would be the correct value to use.Ennead
T
63

Perhaps divider or dividerHeight property of the ListView can solve your problem.

Transcendentalism answered 13/2, 2011 at 13:58 Comment(0)
G
45

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>
Gardel answered 29/5, 2013 at 21:47 Comment(1)
Indeed a better solution. Thanks.Navigator
A
16

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.

Alcatraz answered 7/6, 2012 at 3:10 Comment(4)
i wanted left/right margin so your answer was helpful for me but I don't like the idea of just wrapping another layout only for margin purposeRonnaronnholm
There are a few ways.. and that's the bad one in terms of layouting performance.. try to think at lists with tens (or hundreds) of items :)Spiracle
@andrea.spot it wouldn't matter for lists with tens or hundreds of items unless that was how many items were visible on the screen at once. ListView uses view recycling (if you've implemented your adapter correctly).Extemporaneous
This will not work if the ListView items will have a backgroundCrater
C
12

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

Consuelaconsuelo answered 15/10, 2013 at 4:41 Comment(0)
F
10

For my application, i have done this way

 <ListView
    android:id="@+id/staff_jobassigned_listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@null"
    android:dividerHeight="10dp">

</ListView>

just set the divider to null and providing height to the divider did for me.

Example :

android:divider="@null"

or

android:divider="@android:color/transparent"

and this is result

enter image description here

Foraminifer answered 2/12, 2015 at 8:13 Comment(1)
I think, better is android:divider="@android:color/transparent".Jameljamerson
B
7

If you want to show a divider with margins and without stretching it - use InsetDrawable (size must be in a format, about which said @Nik Reiman):

ListView:

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="#00000000"
    android:divider="@drawable/separator_line"
    android:dividerHeight="10.0px"/>

@drawable/separator_line:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="5.0px"
    android:insetRight="5.0px"
    android:insetTop="8.0px"
    android:insetBottom="8.0px">

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <gradient
            android:startColor="@color/colorStart"
            android:centerColor="@color/colorCenter"
            android:endColor="@color/colorEnd"
            android:type="linear"
            android:angle="0">
        </gradient>
    </shape>
</inset>
Belligerent answered 12/7, 2013 at 20:7 Comment(0)
S
7

You can use:

android:divider="@null"
android:dividerHeight="3dp"

example:

<ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView" android:layout_gravity="center_horizontal"
        android:dividerHeight="3dp"
        android:divider="@null" android:clickable="false"/>
Salsify answered 3/2, 2014 at 12:59 Comment(1)
Combining null with list item layout produces the best effectMcclellan
K
5

I realize that an answer was already been selected, but I just wanted to share what ended up working for me when I ran into this issue.

I had a listView where each entry in the listView was defined by its own layout, similar to what Sammy posted in his question. I tried the suggested approach of changing the divider height, but that did not end up looking all too pretty, even with an invisible divider. After some experimentation, I simply added an android:paddingBottom="5dip" to the last TextView layout element in the XML file that defines individual listView entries.

This ended up giving me exactly what I was trying to achieve via the use of android:layout_marginBottom. I found this solution to produce a more aesthetically pleasing result than trying to increase the divider height.

Kirin answered 7/9, 2012 at 20:36 Comment(0)
P
3

Instead of giving margin, you should give padding:

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:divider="@android:color/green"
    android:dividerHeight="4dp"
    android:layout_alignParentTop="true"
    android:padding="5dp" >

</ListView>

OR

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:paddingTop="2dp"
    android:divider="@android:color/green"
    android:dividerHeight="4dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    android:paddingBottom="2dp"
    android:paddingStart="0dp"
    android:paddingEnd="0dp" >

</ListView>
Plotter answered 3/5, 2015 at 12:7 Comment(0)
M
2

Simplest solution with OP's existing code (list items already have got padding) is to add following code:

listView.setDivider(new ColorDrawable(Color.TRANSPARENT));  //hide the divider
listView.setClipToPadding(false);   // list items won't clip, so padding stays

This SO answer helped me.

Note: You may face a bug of the list item recycling too soon on older platforms, as has been asked here.

Munday answered 7/1, 2015 at 14:25 Comment(2)
you can set divider to null and set dividerHeight to whatever you like.Waits
@Andy I think it will also remove the padding between items. Which will be creating the issue which the OP wanted to solve..Munday
A
2

you just need to make background transparent of list divider and make height according to your needed gap.

<ListView 
         android:id="@+id/custom_list"
         android:layout_height="match_parent"
         android:layout_width="match_parent"
         android:divider="#00ffffff"
         android:dividerHeight="20dp"/>
Aden answered 3/4, 2017 at 12:23 Comment(0)
W
1
<ListView
    android:clipToPadding="false"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:dividerHeight="10dp"
    android:divider="@null"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ListView>

and set paddingTop, paddingBottom and dividerHeight to the same value to get equal spacing between all elements and space at the top and bottom of the list.

I set clipToPadding to false to let the views be drawn in this padded area.

I set divider to @null to remove the lines between list elements.

Waits answered 2/6, 2015 at 8:1 Comment(0)
T
1

Also one more way to increase the spacing between the list items is that you add an empty view to your adapter code by providing the layout_height attribute with the spacing you require. For e.g. in order to increase the bottom spacing between your list items add this dummy view(empty view) to the end of your list items.

<View
    android:layout_width="match_parent"
    android:layout_height="15dp"/>

So this will provide a bottom spacing of 15 dp between list view items. You can directly add this if the parent layout is LinearLayout and orientation is vertical or take appropriate steps for other layout. Hope this helps :-)

Tibold answered 28/8, 2015 at 8:33 Comment(0)
D
1

This will help you add the divider height.

 getListView().setDividerHeight(10)

If you want to add a custom view, you can add a small view in the listView item layout itself.

Dynamotor answered 17/10, 2019 at 6:28 Comment(0)
H
0

In order to give spacing between views inside a listView please use padding on your inflate views.

You can use android:paddingBottom="(number)dp" && android:paddingTop="(number)dp" on your view or views you're inflate inside your listview.

The divider solution is just a fix, because some day, when you'll want to use a divider color (right now it's transparent) you will see that the divider line is been stretched.

Heretofore answered 24/5, 2015 at 8:33 Comment(0)
B
0

I found a not-so-good solution for this in case you are using a HorizontalListView, since dividers don't seem to work with it, but I think it'll work either way for the more common ListView.

Just adding:

<View
android:layout_marginBottom="xx dp/sp"/>

in the bottomest View of the Layout you inflate in the adapter class will create spacing between items

Be answered 16/12, 2015 at 14:24 Comment(0)
C
0

A lot of these solutions work. However, if all you want is to be able to set the margin between items the simplest method I have come up with is to wrap your item - in your case the CheckedTextView - in a LinearLayout and put your margin formatting for the item in that, not the root-layout. Be sure to give this wrapping layout an id and create it along with your CheckedTextView in your adapter.

That's it. In effect, you are instantiating the margin at the item level for the ListView. Because the ListView does not know about any item layout - only your adapter does. This basically inflates the part of the item layout that was being ignored before.

Counterforce answered 13/6, 2017 at 23:59 Comment(0)
D
0

Maybe you can try to add android:layout_marginTop = "15dp" and android:layout_marginBottom = "15dp" in the outermost Layout

Dolerite answered 9/7, 2020 at 1:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.