Boby
Boby

Reputation: 1202

How to remove item padding in listview?

I recently create a listview with an array adapter and customview. here is what I do

 @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Model_order m_order = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.single_item_order, parent, false);
        }
...
}

and here is my single_item_order

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"

    >

<TextView
    android:layout_gravity="center_vertical"
    android:visibility="invisible"
    android:textAlignment="center"
    android:layout_width="0dp"
    android:layout_weight="0"
    android:layout_height="wrap_content"
    android:id="@+id/code"
    android:includeFontPadding="false"
    android:lineSpacingExtra="0dp"

    />

<TextView
    android:includeFontPadding="false"
    android:lineSpacingExtra="0dp"

    android:layout_gravity="center_vertical"
    android:textAlignment="center"
    android:id="@+id/name"
    android:layout_width="0dp"
    android:layout_weight="35"
    android:layout_height="wrap_content" />

<TextView
    android:includeFontPadding="false"
    android:lineSpacingExtra="0dp"

    android:layout_gravity="center_vertical"
    android:textAlignment="center"
    android:id="@+id/price"
    android:layout_width="0dp"
    android:layout_weight="35"
    android:layout_height="wrap_content" />

    <TextView
        android:includeFontPadding="false"
        android:lineSpacingExtra="0dp"

        android:id="@+id/qty"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="10"
        android:textAlignment="center"
         />

    <TextView

    android:textAlignment="center"
    android:id="@+id/total"
    android:layout_width="0dp"
    android:layout_weight="20"
    android:layout_height="wrap_content" />

    <Button
        android:id="@+id/add"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:minHeight="0dp"
        android:minWidth="0dp" />

    <Button
        android:id="@+id/min"
        android:layout_gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:minHeight="0dp"
        android:minWidth="0dp" />

</LinearLayout>

and here is what I get

enter image description here

Now, I am trying to remove padding. here is what I try so far

so how can I remove it ? or at least I can adjust the padding .

Here is my activity main which is containing my listview

<?xml version="1.0" encoding="utf-8"?>


    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/bubble_outerWrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="100"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="0dp"
            android:layout_weight="50"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:clipToPadding="false"
            android:layout_marginBottom="0dp"
            />
        <View

            android:layout_width="0dp"
            android:layout_weight="5"
            android:layout_height="match_parent" />

        <android.support.v7.widget.CardView
            android:layout_gravity="end"
            android:layout_width="0dp"
            android:layout_weight="45"
            android:padding="0dp"
            android:layout_margin="0dp"
            android:layout_height="match_parent">
                <RelativeLayout

                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/side_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical"
                        android:text="Order Information"
                        android:textSize="14sp"
                        android:typeface="serif" />

                        <ListView
                            android:layout_below="@id/side_title"
                            android:layout_marginTop="5dp"
                            android:id="@+id/listss"
                            android:layout_width="match_parent"
                            android:layout_height="250dp" />
    <LinearLayout
        android:layout_below="@id/listss"
    android:layout_centerHorizontal="true"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
                        <Button
                            android:minHeight="0dp"
                            android:minWidth="0dp"
                            android:id="@+id/clear"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="X"
                            />

                        <Button

                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Pay"
                            />

    </LinearLayout>
                </RelativeLayout>
        </android.support.v7.widget.CardView>

    </LinearLayout>

Upvotes: 1

Views: 119

Answers (2)

mehul chauhan
mehul chauhan

Reputation: 1802

Try this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<!--<TextView-->
    <!--android:layout_gravity="center_vertical"-->
    <!--android:visibility="visible"-->
    <!--android:textAlignment="center"-->
    <!--android:layout_width="0dp"-->
    <!--android:layout_weight="0"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:text="asdfa"-->
    <!--android:includeFontPadding="false"-->
    <!--android:lineSpacingExtra="0dp"-->

    <!--/>-->

<TextView
    android:includeFontPadding="false"
    android:lineSpacingExtra="0dp"
    android:layout_gravity="center_vertical"
    android:textAlignment="center"
    android:id="@+id/name"
    android:layout_width="0dp"
    android:layout_weight="35"
    android:layout_height="wrap_content" />

<TextView
    android:includeFontPadding="false"
    android:lineSpacingExtra="0dp"
    android:layout_gravity="center_vertical"
    android:textAlignment="center"
    android:id="@+id/price"
    android:layout_width="0dp"
    android:layout_weight="35"
    android:layout_height="wrap_content" />

<TextView
    android:includeFontPadding="false"
    android:lineSpacingExtra="0dp"
    android:id="@+id/qty"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="10"
    android:textAlignment="center"
    />

<TextView

    android:textAlignment="center"
    android:id="@+id/total"
    android:layout_width="0dp"
    android:layout_weight="20"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/add"
    android:layout_gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:minHeight="0dp"
    android:minWidth="0dp" />

<Button
    android:id="@+id/min"
    android:layout_gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="-"
    android:minHeight="0dp"
    android:minWidth="0dp" />

Upvotes: 0

Pie
Pie

Reputation: 594

A simpler layout would look like this :

  • A vertical LinearLayout to contain the ListView and the action bar
  • The LinearLayout used for the action bar has its height to wrap_content so it only gets the height required by its children
  • The ListView has its height to 0dp and its weight to 1 in order to let it take all the available space left by the action bar
<?xml version="1.0"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawingCacheQuality="high">
    <ListView
        android:id="@+id/messagingListView"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="@null"
        android:layout_margin="0dp"
        android:dividerHeight="10dp"
        android:paddingTop="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="0dp"
        android:paddingLeft="10dp" />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:background="#FFFFFF">
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="new_game"
            android:textColor="#FFFFFF" />
    </LinearLayout>
</LinearLayout>

If space remains, you should definitly show us the layout used for list items

Note :
In Android Studio, try to single click on the LinearLayout to let it be highlighted in the Preview, then on the ListView to see if there is a gap (Use the zoom). If you see a gap with the layout just above, it's an Android Studio Preview bug on your computer, it works on every devices.

Upvotes: 1

Related Questions