Question

I'm trying to center an edittext vertically and horizontally in a linear layout, but it is not working. Very simple problem really.

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" 
 android:background="@drawable/login">



<EditText
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:background="@drawable/loginbutton"
    android:text="Username"
    android:layout_gravity="center"
    android:textSize="25dp"
    android:gravity="center_horizontal|center_vertical"
    android:textColor="#000000"
    android:layout_margin="10dp"
    android:id="@+id/username">
  </EditText>

I'll try changing to a relativelayout in the mean time.

Was it helpful?

Solution

LinearLayout do not support centering in the direction it stacks items as far as I know. Unless the LinearLayout is crucial (which in your case it shouldn't be, as you also need the centering), I would recommend you switch to a RelativeLayout.
With a RelativeLayout, you can set the attribute android:layout_centerInParent="true" on the EditText to get it centred.

OTHER TIPS

Using gravity affects the gravity of the contents of said UI item, this means (in this case) that the text inside the EditText would be centered. In order to center the UI item itself you need to define layout_gravity as this is the gravity a UI item holds within his parent. The code posted by the OP will do the horizontal center but not the vertical center.

As an alternative you can use RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/login"
android:orientation="vertical" >

<EditText
    android:id="@+id/username"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="@drawable/loginbutton"
    android:layout_centerInParent="true"
    android:text="Username"
    android:textColor="#000000"
    android:textSize="25dp" >
</EditText>

</RelativeLayout>

It is very simple. Add android:gravity="center" with the linear layout.

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/login"
android:gravity="center"

Try this:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" 
 android:background="@drawable/login"
 android:layout_centerVertical="true"
 android:layout_centerInParent="true">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top