Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center vertically the ConstraintLayout content in Android Studio?

I'm begginer with Android Studio and I'm trying to center vertically content of ConstraintLayout, but without success. I've tried to use android:gravity="center" and I removed the app:layout_editor_absoluteY of all the elements, but not happens... So, how can I centralize this?

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@layout/gradient"
    android:gravity="center"
    tools:context="com.test.test.MainActivity"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <EditText
        android:id="@+id/txtEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:ems="10"
        android:hint="@string/lblEmail"
        android:inputType="textEmailAddress"
        android:textColorHint="@color/colorGray"
        android:background="@color/colorWhite"
        android:padding="15dp" />

    <EditText
        android:id="@+id/txtSenha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="@string/lblSenha"
        android:inputType="textPassword"
        android:textColorHint="@color/colorGray"
        android:background="@color/colorWhite"
        android:padding="15dp"
        app:layout_constraintTop_toBottomOf="@+id/txtEmail" />

    <Button
        android:id="@+id/btnEntrar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/lblEntrar"
        app:layout_constraintTop_toBottomOf="@+id/txtSenha" />

</android.support.constraint.ConstraintLayout>
like image 862
Igor Avatar asked Apr 12 '17 17:04

Igor


People also ask

How do I center text in ConstraintLayout in Android?

Just set android:layout_width="0dp" and add constraints app:layout_constraintStart_toStartOf="parent" & app:layout_constraintEnd_toEndOf="parent" , it'd just work fine.

How do I center a view in ConstraintLayout?

You can use layout_constraintCircle for center view inside ConstraintLayout . with constraintCircle to parent and zero radius you can make your view be center of parent. Save this answer.


1 Answers

ConstraintLayout is pretty flexible in the arrangements you can make, but from your description I think a "Packed Chain" would fit what your asking for.

A "Chain" means that all the Views in a line constrain to each other in both directions, so the top view constrains to the next view down and that next view constrains back up to the top view and so on down the chain. You then have some options for how the chain behaves. So in your example(extra stuff removed for simplicity):

<android.support.constraint.ConstraintLayout
 ....
 android:layout_width="match_parent"
 android:layout_height="match_parent">

    <EditText
        android:id="@+id/txtEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/txtSenha"
        app:layout_constraintVertical_chainStyle="packed" />

    <EditText
        android:id="@+id/txtSenha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintTop_toBottomOf="@+id/txtEmail"
        app:layout_constraintBottom_toTopOf="@+id/btnEntrar"/>

    <Button
        android:id="@+id/btnEntrar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintTop_toBottomOf="@+id/txtSenha"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>

In a chain, the top view(or left for horizontal chains) is the "head" where you can change the behaviour of the chain, in this case the chainStyle is packed meaning the views all cram together. By default they will be packed in the centre of the screen, but you can change this by altering the attributes in the "head" view, eg app:layout_constraintVertical_bias="0.9" would put them 90% of the way down the screen instead.

More about chains (including diagrams) in the ConstraintLayout guidance here.

like image 165
Lewis McGeary Avatar answered Oct 12 '22 18:10

Lewis McGeary