VansFannel
VansFannel

Reputation: 45921

Center list item vertically on ListView

I'm developing an Android application.

On one activity I have a List with list items. Those list items will have a TextView (I'm using my own layout because probably I will need to add more widgets to it).

This is ListActivity xml:

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

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="469dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="40dp">
    </ListView>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/orderErrorMsg"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="16dp" >
        </TextView>
    </RelativeLayout>

</LinearLayout>

And this is ListItem xml:

<?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="100dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/orderToFillName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:textSize="20dp" />

</LinearLayout>

And, finally, this is the result:

enter image description here

Why TextView is not centered vertically?

Upvotes: 2

Views: 11274

Answers (3)

DecodeGnome
DecodeGnome

Reputation: 1809

You have "wrap_content" on the layout_height of the TextView = there is no space to center vertically on since the TextView is wrapped tightly.

Try changing the layout_height to "match_parent" or place gravity="center_vertical" attribute in the LinearLayout instead.

<TextView
        android:id="@+id/orderToFillName"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:textSize="20dp" />

Upvotes: 4

Tim
Tim

Reputation: 6712

the answer is really simple: you have to add this to your TextView's XML:

android:gravity="center"

Currently you're not setting the text's position in the TextView but just the View's position.

Best wishes, Tim

Upvotes: 1

Adil Soomro
Adil Soomro

Reputation: 37729

Try this way:

<TextView
        android:id="@+id/orderToFillName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity= "center_vertical|center_horizontal"
        android:textSize="20dp" />

or

android:gravity="center"

Upvotes: 0

Related Questions