Search code examples
androidlayoutandroid-imageviewandroid-constraintlayoutandroid-image

ImageView doesn't center on ConstraintLayout


I can't move the image more to the left side no matter what I'm doing. It should be nicely centered but it's not... image

<ImageView
        android:id="@+id/grid"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/back3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/turn"
        app:srcCompat="@drawable/back3"
        android:layout_gravity="center"/>

Any ideas?


Solution

  • When you chain your view to two points using start/end constraints, it stays in the center of those constraints by default. But have the ability to shift it's position toward end or start the layout attribute responsible for this is layout_constraintHorizontal_bias As @Robert mentioned, you can remove that attribute or set it to 0.5

    <ImageView
        android:id="@+id/grid"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/back3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/turn"
        app:srcCompat="@drawable/back3"
        android:layout_gravity="center"/>
    

    Also I suggest you to use AppCompatImageView instead