How to set ImageView to the right side of ConstraintLayout

Viewed 1301

I have a image view which I want to set it to the horizontal right side in constraint layout, but the layout_constraintEnd_toEndOf constraint seems not work in imageView.

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/text_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:textSize="24sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0"
            tools:text="text_value" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_baseline_keyboard_arrow_right_24" />
2 Answers

Replace words Start and End with Right and Left according to your needs:

 <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRight="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_baseline_keyboard_arrow_right_24" />

Using words Start and End when adjusting views in a layout means the layout direction may change according to locale, but when you use the words Right and Left, it means you are specifying the absolute places of views.

Constraint textView at the most start, most top and most bottom of the parent but also its right is left to imageView.

<TextView
android:id="@+id/textView" 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp" 
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp" 
android:textSize="24sp"
app:layout_constraintRight_toLeftOf="+id/imageView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />

And, constraint ImageView at the most end, most top and most bottom of the parent but also its left is right to textView.

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" 
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constrainLeft_toRightOf="+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:srcCompat="@drawable/ic_baseline_keyboard_arrow_right_24" />
Related