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.
To center a view, just drag the handles to all four sides of the parent.
To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.
android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.
I track the answer on Google groups, here it is © Romain Guy:
Well, first of all RelativeLayout ignores layout_gravity. Then you need to know that gravity means "apply gravity to the content of this view" whereas layout_gravity means "apply gravity to this view within its parent." So on a TextView, gravity will align the text within the bounds of the TextView whereas layout_gravity will align the TextView within the bounds of its parent.
There are three ways you can center a view vertically. I recommend using the ConstraintLayout now.
The key is orientation="horizontal". You can't center horizontally for orientation="vertical".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="example text"/> </LinearLayout>
With a RelativeLayout you can use layout_centerVertical="true"
.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="example text"/> </RelativeLayout>
It is easiest to show you this one.
Here is the XML. It still would need a horizontal constraint to be added.
<android.support.constraint.ConstraintLayout 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" tools:context="com.example.constraintlayout.MainActivity" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="81dp"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:text="example text" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" tools:layout_editor_absoluteX="16dp"/> </android.support.constraint.ConstraintLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With