Home > Net >  Center the ConstraintLayout in the screen
Center the ConstraintLayout in the screen

Time:07-19

I'm trying to vertically center a ConstraintLayout in the screen. The ConstraintLayout is the root element of the entire layout of the LoginActivity.

I set the android:layout_gravity to center (as in the code below). It does the job in the Android Studio Design Editor, but when I run the app on the phone the ConstraintLayout is aligned to the top of the screen.

How can I effectively center the ConstraintLayout in the screen? Thanks.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
    android:layout_gravity="center"
    tools:context=".activities.LoginActivity">

    <ImageView
        android:id="@ id/ivLogo"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_marginTop="50dp"
        android:contentDescription="@string/cutitapp_logo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_cutitapp_logo"
        tools:srcCompat="@drawable/ic_cutitapp_logo" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@ id/tilEmail"
        style="@style/CutitApp.TextInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="52dp"
        android:layout_marginEnd="24dp"
        app:endIconMode="clear_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/ivLogo">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@ id/etEmail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints=""
            android:hint="@string/email"
            android:inputType="textEmailAddress"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:ignore="TextContrastCheck" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@ id/tilPassword"
        style="@style/CutitApp.TextInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tilEmail"
        app:passwordToggleEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@ id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints=""
            android:hint="@string/password"
            android:inputType="textPassword"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/etEmail"
            tools:ignore="TextContrastCheck" />

    </com.google.android.material.textfield.TextInputLayout>

    <TextView
        android:id="@ id/tvError"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="40dp"
        android:layout_marginTop="16dp"
        android:textColor="?attr/colorError"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/tilPassword" />

    <Button
        android:id="@ id/btnLogin"
        style="@style/CutitApp.Button"
        android:layout_width="0dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="24dp"
        android:text="@string/login"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/tvError" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/btnLogin">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/not_a_member" />

        <Button
            android:id="@ id/btnRegister"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/register"
            android:textAllCaps="false"
            android:textColor="@color/design_default_color_primary"
            android:textSize="14sp" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Here you go.

I just added an extra ConstraintLayout


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
    android:layout_gravity="center">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <ImageView
            android:id="@ id/ivLogo"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:layout_marginTop="50dp"
            android:contentDescription="@string/cutitapp_logo"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_cutitapp_logo"
            tools:srcCompat="@drawable/ic_cutitapp_logo" />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/tilEmail"
            style="@style/CutitApp.TextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="52dp"
            android:layout_marginEnd="24dp"
            app:endIconMode="clear_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/ivLogo">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@ id/etEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:autofillHints=""
                android:hint="@string/email"
                android:inputType="textEmailAddress"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                tools:ignore="TextContrastCheck" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@ id/tilPassword"
            style="@style/CutitApp.TextInputLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tilEmail"
            app:passwordToggleEnabled="true">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@ id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:autofillHints=""
                android:hint="@string/password"
                android:inputType="textPassword"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/etEmail"
                tools:ignore="TextContrastCheck" />

        </com.google.android.material.textfield.TextInputLayout>

        <TextView
            android:id="@ id/tvError"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="40dp"
            android:layout_marginTop="16dp"
            android:textColor="?attr/colorError"
            android:textSize="12sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/tilPassword" />

        <Button
            android:id="@ id/btnLogin"
            style="@style/CutitApp.Button"
            android:layout_width="0dp"
            android:layout_marginStart="24dp"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="24dp"
            android:text="@string/login"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/tvError" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginEnd="24dp"
            android:gravity="center"
            android:orientation="horizontal"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/btnLogin">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/not_a_member" />

            <Button
                android:id="@ id/btnRegister"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/register"
                android:textAllCaps="false"
                android:textColor="@color/design_default_color_primary"
                android:textSize="14sp" />

        </LinearLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
    
</androidx.constraintlayout.widget.ConstraintLayout>

  • Related