Jasper
Jasper

Reputation: 8705

Android: Vertically Center TextView in LinearLayout

Using the following layout, i am unable to Vertically center the TextView:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"    
    android:gravity="center_vertical"   
    android:background="#323331">

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"  
        android:text="Hello World" 
        android:textColor="#FFFFFF" 
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_gravity="center_vertical"       
        android:gravity="center_horizontal|center_vertical"             
        />     
</LinearLayout>

The text always aligns to the top of the LinearLayout.
(If it matters this layout is used for title of Activity)
How do i align it in Vertical Center fashion?

Upvotes: 2

Views: 7715

Answers (3)

Palak
Palak

Reputation: 574

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#323331"
android:orientation="horizontal" >

<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:text="Hello World"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFFFFF" />

</LinearLayout>

Upvotes: 0

elixir bash
elixir bash

Reputation: 280

Try this Android:layout_height="fill_parent"

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#323331"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center_horizontal|center_vertical"
        android:text="Hello World"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FFFFFF" />

</LinearLayout>

Upvotes: 0

grig
grig

Reputation: 847

Your LinearLayout have android:layout_height="wrap_content" so it fit the height of your TextView. Change

android:layout_height="wrap_content"

to

android:layout_height="match_parent"

Upvotes: 7

Related Questions