Android ConstraintLayout: set one element in the center of another one

I’m using ConstraintLayout in order to create activity view

There is a code snippet:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical"
    tools:context=".presentation.screens.registration.RegistrationActivity">

    <LinearLayout>...</LinearLayout>

    <Button
        android:id="@+id/login_next_btn"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@drawable/button_green_disabled"
        android:layout_width="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="24dp"
        android:text="@string/next"
        android:textColor="@color/font_white"
        android:textSize="16sp"
        android:layout_height="48dp"
        android:enabled="false"
        android:onClick="onNextClick"/>

    <ImageView
        android:id="@+id/loader_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/login_next_btn"
        android:src="@drawable/ic_loader"/>


</androidx.constraintlayout.widget.ConstraintLayout>

The question is: How can I set ImageView (android:id="@+id/loader_image") in the center of Button (@+id/login_next_btn)?

>Solution :

If you have to set it in the center just change the constraints of the image with :

 app:layout_constraintBottom_toBottomOf="@id/login_next_btn"
 app:layout_constraintTop_toTopOf="@id/login_next_btn"
 app:layout_constraintStart_toStartOf="@id/login_next_btn"
 app:layout_constraintEnd_toEndOf="@id/login_next_btn"

Leave a Reply