Amrmsmb
Amrmsmb

Reputation: 11416

how to add space between items in listview

The below mentioned layout is a model of one item in a ListView. As you see it, the three TextViews are separated from each as each one occupies .3 of the entire row space.

The problem is, when I add items to the ListView I found that the three TextViews are just linked to each other. For example, let's assume I want to add item to the ListView contains the following:

Adam USA M

I expect to see that row on the screen with some spaces separating each textView, but what happens is that I get something like the following:

AdamUSAM

Why this is happening and how to solve it?

model_view:

<?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="match_parent">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="Name"/>

    <TextView
        android:id="@+id/tvAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="Address: "/>

    <TextView
        android:id="@+id/tvGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight=".3"
        android:text="gender: "/>
</LinearLayout>

Update:

Now I changed the layout to be as follows, but the problem is persists which is that the three textviews are appearing clinching to each other without spacing.

layout

<?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="match_parent"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Name: "
        android:focusable="false"/>

    <TextView
        android:id="@+id/tvAddress"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Address: "
        android:focusable="false"/>

    <TextView
        android:id="@+id/tvGender"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="gender: "
        android:focusable="false"/>
</LinearLayout>

screen shot:

enter image description here

Upvotes: 0

Views: 570

Answers (5)

Rishabh Kumar
Rishabh Kumar

Reputation: 557

I would suggest to remove layout_weight from TextViews, define a specific height to them and add padding. Adding margins may cause your TextViews to go out of ListView.

Upvotes: 0

Enzokie
Enzokie

Reputation: 7415

You need to specify the orientation of LinearLayout e.g. android:orientation="horizontal" then replace your weights to 1 instead of 0.3 and make sure your TextView width is 0dp not wrap_content.

Upvotes: 2

mohammadreza khalifeh
mohammadreza khalifeh

Reputation: 1618

in your listView xml add :

android:dividerHeight="8 dip"

or any value you want.

Upvotes: 0

Bryan
Bryan

Reputation: 15166

Add a layout_margin or padding to each TextView.

Upvotes: 1

Ibrahim Ali Khan
Ibrahim Ali Khan

Reputation: 92

Use property android:layout_margin="10dp"

Upvotes: 0

Related Questions