0

I am trying to add spacing to my list items in ListView. I have tried using divider and dividerHeight but this doesn't seem to work. Please what could be wrong?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_export_logistics"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.orume.export.ExportLogisticsActivity">


<include layout="@layout/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></include>


<ListView
    android:id="@+id/ListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:divider="@null"
    android:dividerHeight="16dp"
    android:layout_marginTop="65dp"
    android:background="#fff"></ListView>
</RelativeLayout>

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="20dp"
    android:color="#dd0d0d" />

</shape>

4 Answers4

1

you don't set your divider to your ListView.
android:divider="@null" @null here means that you set the divider to empty. Now you have a drawable divider.xml, and you need to tell Android that you want it to be used as a divider.

If your divider.xml is placed under drawable directory, then use android:divider="@drawable/divider"

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
0

Ok. Found a way out. I ended up add android:layout_marginBottom to the texView that defines my ListView and it worked perfectly. Thanks.

  • how does it set the divider to the drawable, you wanted? – Vladyslav Matviienko Nov 29 '16 at 11:46
  • The aim was to add spacing between the list items using the divider set to drawable. But that didn't work for what i wanted so i added marginBottom to the TextView for my ListView. @VladMatvienko –  Nov 29 '16 at 11:58
  • well, that is not a *correct* solution as for me. I'd suggest you to use `dividerHeight`, and set `divider` to the transparent color. With your solution it will add etra space even after the last list item – Vladyslav Matviienko Nov 29 '16 at 12:04
  • Tried all that before coming to SO. Am aware of the spacing after the list item. I used this beacuse the activity contains only ListView. Maybe i should add that to my solution. Anyways thanks for your help @VladMatvienko –  Nov 29 '16 at 12:07
-1

Create a custom view for your list items and give top and bottom padding or other space adjustments inside that custom item layout to bring space for list items. An eg link is below Android custom Row Item for ListView

Also another note is giving divider height might not work well.

Community
  • 1
  • 1
Febi M Felix
  • 2,799
  • 1
  • 10
  • 13
-2
<ListView
    android:id="@+id/ListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="16dp"
    android:layout_marginTop="65dp"
    android:background="#fff"></ListView>

replace this listview with yours..

Nikhil Borad
  • 2,065
  • 16
  • 20