Need help centering an EditText in a Linear Layout
Asked Answered
S

4

6

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.

Slipknot answered 24/1, 2012 at 15:37 Comment(0)
I
12

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.

Inshrine answered 24/1, 2012 at 15:53 Comment(4)
now how could I center all 3 on the screen? Because I center one, and put the 2 below it, but it looks silly. I need all 3 to be centered, almost like they were in one view and I center THAT view in the parent. Any ideas?Slipknot
Center just one of them, and then use the above and below tags on the other two using only horizontal centering.Notice
I did center just one, this is what it looks like... I need to have all 3 centered as a whole so there is equal distance from the username to the top and the login button to the bottom freeimagehosting.net/ktxbpSlipknot
In your screenshot it looks like 'username' is the centred view, if you center '....' instead, and put 'username' above it (layout_above), and 'Login' below (layout_below). If this is not exact enough, you can do as you hinted at; put all of the views in a vertical LinearLayout that is inside the RelativeLayout, and set centerInParent="true" on the LinearLayout.Inshrine
N
1

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>
Notice answered 24/1, 2012 at 15:39 Comment(0)
H
1

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"
>
Hydrometeor answered 28/12, 2017 at 8:56 Comment(0)
C
-1

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">
Celle answered 24/1, 2012 at 15:40 Comment(4)
that exact code didnt work. should i add those 2 lines in the edittext ?Slipknot
You could try, but you could also try adding 'android:layout_gravity="center_horizontal|center_vertical"'Celle
android:layout_centerVertical="true" android:layout_centerInParent="true" Is used when inside a relative layout, setting those in the linear layout would not do anything.Notice
Oh cool, the more you know. I've got a layout with the code above working fine, but I may just be lucky.Celle

© 2022 - 2024 — McMap. All rights reserved.