Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Listview remove any space between items

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

like image 254
Phil Avatar asked Jun 23 '14 18:06

Phil


1 Answers

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>
like image 167
David S. Avatar answered Sep 22 '22 12:09

David S.