How to place imageview on the right end in the linear layout horizontal orientation

58 Views Asked by At

Code:(Image Attached)I am using LinearLayout and gravity and weight is not working for me in this case.[![enter image description here][1]][1] I want to place heart icon at the right end of the row. but it is displayed next to the end of textview "

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/item_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.cardview.widget.CardView
            android:id="@+id/item_card"
            android:layout_width="170dp"
            android:layout_height="150dp"
            android:layout_marginStart="18dp"
            app:cardCornerRadius="4dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/list_img"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/icon_check_circle"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/tv_item_duration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/list_img"
                android:layout_alignTop="@+id/list_img"
                android:layout_margin="2dp"
                android:gravity="center"
                android:text="Hello"
                android:textColor="@color/white" />

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/img_share"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignEnd="@+id/item_duration"
                android:layout_gravity="end"
                android:layout_margin="2dp"
                android:src="@drawable/icon_share" />

        </androidx.cardview.widget.CardView>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="18dp"
        android:layout_marginTop="2dp"
        android:gravity="right"
        android:orientation="horizontal"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/item_layout">

        <TextView
            android:id="@+id/tv_item_desc"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:layout_weight="1"
            android:ellipsize="end"
            android:maxLength="16"
            android:maxLines="1"
            android:text="fan"
            android:textSize="16sp"
            android:textStyle="bold" />

        <androidx.appcompat.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginEnd="2dp"
            android:src="@drawable/icon_favorite" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

"

I want to place imageview on the right end in the linear layout horizontal orientation [1]: https://i.stack.imgur.com/9W8pB.jpg

2

There are 2 best solutions below

0
Shiro Ennosuke On BEST ANSWER

Very Simple. Please change your layout_width of TextView into 0dp and layout_width of LinearLayout into match_parent. Or add this line into your LinearLayout.

app:layout_constraintEnd_toEndOf="parent"
0
Nikhil Sharma On

You can achieve this by setting the width of your linear layout to 0dp. Also you need to add

   app:layout_constraintEnd_toEndOf="@id/item_layout"

to your linear layout in question. Complete code of updated xml is

<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<RelativeLayout
    android:id="@+id/item_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.cardview.widget.CardView
        android:id="@+id/item_card"
        android:layout_width="170dp"
        android:layout_height="150dp"
        android:layout_marginStart="18dp"
        app:cardCornerRadius="4dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/list_img"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/icon_check_circle"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/tv_item_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/list_img"
            android:layout_alignTop="@+id/list_img"
            android:layout_margin="2dp"
            android:gravity="center"
            android:text="Hello"
            android:textColor="@color/white"
            />

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/img_share"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignEnd="@+id/item_duration"
            android:layout_gravity="end"
            android:layout_margin="2dp"
            android:src="@drawable/icon_share" />

    </androidx.cardview.widget.CardView>
</RelativeLayout>

<LinearLayout
    android:id="@+id/item_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="18dp"
    android:layout_marginTop="2dp"
    android:orientation="horizontal"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="@id/item_layout"
    app:layout_constraintTop_toBottomOf="@id/item_layout">

    <TextView
        android:id="@+id/tv_item_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLength="16"
        android:maxLines="1"
        android:text="fan"
        android:textSize="16sp"
        android:textStyle="bold" />

    <androidx.appcompat.widget.AppCompatImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginEnd="2dp"
        android:src="@drawable/icon_favorite" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>