Vertically centering a view in Android

Viewed 26465

I'm trying to center a View vertically on screen with the following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <EditText 
        android:text="example text"  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />
</LinearLayout>

However it doesn't work. The EditText is still at the top of the screen. Can someone explain what I'm doing wrong here?

NOTE: if I add center_horizontal to the layout_gravity attribute then it centers it horizontally, but still does not center vertically.

UPDATE: using android:gravity="center_vertical" on the parent worked. I still don't understand why android:layout_gravity="center_vertical" on the child didn't work.

3 Answers
Related