0

I made a ListView in Android with some arbitrary elements. Row layout is inflated from XML file. Everything works fine but there is an extra margin bottom added to each row by default which i never mentioned anywhere.

Intentionally i set ListView background as green and row background as blue and RelativeLayout background as red. The result being there is a 1/1.5 DP margin bottom applied to each row.

Here the border with green color is actually the background color of ListView which indicates there is some gap between two rows.

Is this an intended behavior in android ListView if so why they add margin bottom automatically to rows ? And how to remove/overcome this behavior ?

Thank You, Have a nice day <3

Screenshot -

enter image description here

MainActivity.java

package com.xxx.abcd;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String[] num = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"};
        ListView lv = (ListView) findViewById(R.id.lv);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, R.layout.row, num);
        lv.setAdapter(arrayAdapter);
    }

}

activity_main.xml

<?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_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    tools:context="com.xxx.abcd.MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00FF00">

    </ListView>
</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:background="#214181"
    android:textColor="#FFFFFF"
    android:textSize="24sp"
    android:padding="10dp">

</TextView>
HeiseN
  • 101
  • 1
  • 1
  • 8

2 Answers2

1

Yes, is the intende behavior for a ListView, this is not anymore true for a recyclerview.

You have to disable the divider as specified here: https://stackoverflow.com/a/1914588/802034

getListView().setDivider(null);
getListView().setDividerHeight(0);



android:divider="@null"
android:dividerHeight="0dp"
Community
  • 1
  • 1
Luigi Papino
  • 408
  • 3
  • 12
0

Firs of all its not the margin that is causing the problem. Its the separator in the list view.

Set it to null as.

 <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#00FF00">
</ListView>
Hari Lamichhane
  • 520
  • 5
  • 11