Skip to content Skip to sidebar Skip to footer

How To Give Empty Space Between Rows Of Listview?

In my application I have a requirement of listview with empty spaces between rows of list.So that I can give background to each row and it will look something like blocks of rows.

Solution 1:

You can use android:divider and android:dividerHeight to customize the spacing between rows.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <ListView 
    android:id="@+id/android:list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:divider="#FF0000"
    android:dividerHeight="4px"/>

</LinearLayout>

Solution 2:

I had the same problem except I needed to set the divider programatically because the view was dynamically generated. I'm posting this answer because it was my first google hit for future reference.

You need a custom Drawable.
Create a new file with the following code:
drawable/list_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <size
        android:height="16dp"/>
    <solid android:color="@android:color/transparent"/>
</shape>

Inside your java code you need to get a reference to your ListView and set the divider like this:

listView.setDivider(getResources().getDrawable(R.drawable.list_divider));

The result is exactly the same.


Post a Comment for "How To Give Empty Space Between Rows Of Listview?"