Search code examples
androidandroid-layoutandroid-constraintlayout

How to align one view to the center of another view in constraint layout


enter image description here

In the above image, I have to align these dots (which are just empty View in XML) to the center of the icon which is above it

The below code is my recycler view which contains the view's

<androidx.core.widget.NestedScrollView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/itemImage">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nestedRecyclerView"/>

</androidx.core.widget.NestedScrollView>

and the above code is for icon

 <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/itemImage"
        android:src="@drawable/ic_glucose"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

Now I need to align "NestedScrollView" to the center of "ImageView" so that the dots can align to the center of the icon.

if I set

app:layout_constraintLeft_toLeftOf="nestedScrollView"

it will align it to the complete left, But i want it to be aligned to the center of the icon

How can i do that, any suggestion please


Solution

  • It's not possible to directly align the NestedScrollView's left to the center of your ImageView, but there is a way.

    You can align an empty View to the center of your ImageView (right to right side and left to left side of ImageView). Then, your NestedScrollView can simply align to this centered View, and by aligning RecyclerView to the left of NestedScrollView you will have the desired results.