androidandroid-layoutandroid-linearlayout

Fixing an ImageView to right-most place in a LinearLayout


So, this is my current LinearLayout:
enter image description here

with the XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp">

        <TextView
            android:id="@+id/crime_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Crime Title" />

        <TextView
            android:id="@+id/crime_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Crime date" />
    </LinearLayout>

    <ImageView
        android:id="@+id/crime_police"
        android:layout_width="117dp"
        android:layout_height="50dp"
        android:layout_gravity="fill_horizontal"
        android:baselineAlignBottom="false"
        android:src="@mipmap/handcuffs" />
</LinearLayout>  

This is the closest I have gotten to my objective, which is to move the ImageView to the right-most in this LinearLayout. So that I can be able to create this following sample using a RecyclerView:
enter image description here

Is it possible to achieve this by using LinearLayout only? Or do I have to look into using other types of Layouts in addition to the LinearLayout which consists of Crime Title and Crime date?
Thanks in advance.


Solution

  • No need to change anything in your layout

    Just Use android:layout_weight="1" inside your second LinearLayout it will work

    Try this

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="8dp">
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="8dp">
    
            <TextView
                android:id="@+id/crime_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Crime Title" />
    
            <TextView
                android:id="@+id/crime_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Crime date" />
        </LinearLayout>
    
        <ImageView
            android:id="@+id/crime_police"
            android:layout_width="117dp"
            android:layout_height="50dp"
            android:layout_gravity="fill_horizontal"
            android:baselineAlignBottom="false"
            android:src="@drawable/ic_menu_send" />
    </LinearLayout>
    

    OUTPUT

    enter image description here