Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center the elements in ConstraintLayout

I am using ConstraintLayout in my application to make applications layout. I am trying to a create a screen wheren one EditText and Button should be in center and Button should be below of EditText with a marginTop only 16dp.

Here is my layout and screenshot how it is looking right now.

activity_authenticate_content.xml

<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:paddingLeft="16dp"     android:paddingRight="16dp"     tools:context="com.icici.iciciappathon.login.AuthenticationActivity">      <android.support.design.widget.TextInputLayout         android:id="@+id/client_id_input_layout"         android:layout_width="0dp"         android:layout_height="wrap_content"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toTopOf="parent">          <android.support.design.widget.TextInputEditText             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:hint="@string/login_client_id"             android:inputType="textEmailAddress" />      </android.support.design.widget.TextInputLayout>      <android.support.v7.widget.AppCompatButton         android:id="@+id/authenticate"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_marginTop="16dp"         android:text="@string/login_auth"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="@id/client_id_input_layout"         app:layout_constraintRight_toRightOf="@id/client_id_input_layout"         app:layout_constraintTop_toTopOf="@id/client_id_input_layout" />  </android.support.constraint.ConstraintLayout> 

enter image description here

like image 824
N Sharma Avatar asked Mar 31 '17 14:03

N Sharma


People also ask

How do I center an image in ConstraintLayout Android?

Just add android:gravity="center" in your layout and done :) Show activity on this post.

How do I center align a view in constraint layout?

Center a view horizontally, set layout_constraintLeft_toLeftOf and layout_constraintRight_toRightOf to parent. Center a view vertically, set layout_constraintTop_toTopOf and layout_constraintBottom_toBottomOf to parent. Align to the top left.


1 Answers

There is a simpler way. If you set layout constraints as follows and your EditText is fixed sized, it will get centered in the ConstraintLayout:

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

The left/right pair centers the view horizontally and top/bottom pair centers it vertically. This is because when you set the left, right or top,bottom constraints bigger than the view it self, the view gets centered between the two constraints i.e the bias is set to 50%. You can also move view up/down or right/left by setting the bias your self. Play with it a bit and you will see how it affects the views position.

like image 103
binW Avatar answered Sep 19 '22 13:09

binW