0

I have been messing around trying to get my login box to stay centered vertically and for some reason it's not working. If I change my ConstraintLayout height to wrap_content the login form is centered, but the background image is also squished down like this:

image

The background image is supposed to stay full screen but because the master constraint layout is wrap content, it shrinks its background. Heres what happens if I set it to fill_parent. We get a full screen image, but the block is no longer centered either.

enter image description here

I currently have the following structure:

Constraint Layout
  -> Linear Layout
    -> Scroll View
      -> login form picture, fields, and button

I am setting them all to wrap, using the gravity AND layout_gravity to center, center_vertical, etc, and I am still not able to center this image. Any suggestions? Here is the current XML for my activity_login layout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:background="@drawable/background"
    android:weightSum="1">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="fill_vertical|center"
        android:orientation="vertical"
        android:padding="46dp">
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center">
            <LinearLayout
                android:id="@+id/email_login_form"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:descendantFocusability="beforeDescendants"
                android:focusableInTouchMode="true"
                android:gravity="center"
                android:orientation="vertical">
                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/login_image"
                    app:srcCompat="@drawable/login_logo" />
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="30dp">
                    <AutoCompleteTextView
                        android:id="@+id/email"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/formbg"
                        android:ems="10"
                        android:hint="@string/prompt_email"
                        android:imeActionId="1"
                        android:imeOptions="actionNext"
                        android:inputType="textEmailAddress"
                        android:maxLines="1"
                        android:padding="16dp" />
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <EditText
                        android:id="@+id/password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/formbg"
                        android:hint="@string/prompt_password"
                        android:imeActionId="2"
                        android:imeActionLabel="@string/action_sign_in_short"
                        android:imeOptions="actionUnspecified"
                        android:inputType="textPassword"
                        android:maxLines="1"
                        android:padding="16dp" />
                </android.support.design.widget.TextInputLayout>
                <Button
                    android:id="@+id/email_sign_in_button"
                    style="?android:textAppearanceSmall"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="30dp"
                    android:background="#9f3737"
                    android:text="@string/action_sign_in"
                    android:textColor="#ffffff"
                    android:textStyle="bold" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>
Kaboom
  • 674
  • 6
  • 27
  • Possible duplicate of [How to center the elements in ConstraintLayout](https://stackoverflow.com/questions/43143468/how-to-center-the-elements-in-constraintlayout) – grrigore Nov 16 '18 at 22:21

1 Answers1

1

Adding this to the child of a ConstraintLayout should center the child both horizontally and vertically:

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

Also take note that: the fill_parent was renamed match_parent in API Level 8 and higher.

grrigore
  • 1,050
  • 1
  • 21
  • 39