Android Listview remove any space between items

10,811

Solution 1

The line android:dividerHeight="10dp" causes the gaps between your lines. I color-coded the overall UI:

Color coded UI

Once I set the dividerHeight line above from "10dp" to "0dp" I got this:

Color coded- no dividerHeight

Ok, so here is the full set of code I used so you can see where you may have gone wrong.

MainActivity:

package com.ds.listviewtest;

import android.app.ListActivity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    static final String[] FRUITS = new String[] {
            "Apple", "Avocado", "Banana",
            "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
            "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setListAdapter(new SimpleAdapter(FRUITS));
        setContentView(R.layout.activity_main);
    }

    private class SimpleAdapter implements ListAdapter
    {
        String[] items;

        public SimpleAdapter(String[] items)
        {
            this.items = items;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return items[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.list_item, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.comment);
            textView.setText(items[position]);

            return rowView;
        }

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 1;
        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean areAllItemsEnabled() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return true;
        }

    }

}

Here is list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@android:color/holo_orange_dark"
    android:id="@+id/wrapper"
    android:padding="0dp"
    android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/comment"
            android:background="@android:color/holo_blue_light"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:text="This is a test comment"
            android:textColor="#000000"/>

</LinearLayout>

Finally here is activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/holo_green_light"
        android:dividerHeight="0dp" />  <!-- EDIT THIS VALUE HERE TO 0DP -->

</RelativeLayout>

Solution 2

Simply, give your divider height a negative value.
Example :
android:dividerHeight="-20dp"
This will remove spaces between ListView values.

Share:
10,811
Phil
Author by

Phil

Updated on June 16, 2022

Comments

  • Phil
    Phil almost 2 years

    I want to remove any space between different items in a ListView. Code:

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:id="@+id/wrapper"
        android:padding="0dp"
        android:layout_height="wrap_content" >
    
            <TextView
                android:id="@+id/comment"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="10dip"
                android:paddingRight="10dip"
                android:textColor="#000000"/>
    
    </LinearLayout>
    

    And Listview

    <ListView
            android:id="@+id/listView1"
            android:transcriptMode="alwaysScroll"
            android:stackFromBottom="true"
            android:dividerHeight="0dp"
            android:divider="@null"
            android:listSelector="@android:color/transparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    

    But there is still some space between the items. Can anyone help me?

    Screenshot

  • Phil
    Phil almost 10 years
    Thats exactly what I was looking for, but look: I already set the height to 0dp o.O
  • David S.
    David S. almost 10 years
    I added the code I used. You should be able to create a new Android app, replace MainActivity with the one above, and add in the two layout XML files and see exactly what I've shown above. Hopefully this will help.
  • Phil
    Phil almost 10 years
    Thank you very much, that helped me. I found my mistake
  • Malav Shah
    Malav Shah over 7 years
    This was not my problem, but your catch to colour the background of all element with different colours helped me a lot with some similar issue. I was getting crazy as where i was making mistake, and colouring it gave me the exact position where the mistake was and now working like a charm.. Kudos..