Search code examples
androidandroid-layoutandroid-linearlayoutgravitylayout-gravity

why android:layout_gravity="right" not working here in android xml


my xml code for list view's row is

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2"
        android:gravity="center" >

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:background="@drawable/app_icon_17"
            android:contentDescription="@string/empty" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="2"
        android:gravity="center" >

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/btn_delete_new"
            android:focusable="false" />
    </LinearLayout>

</LinearLayout>

I want the last button with id btn_delete to stay right aligned. And its showing as I wanted in "graphical layout" while designing. But when I run it, on emulator it's not working.

See the output :

enter image description here

So why the delete button not getting right aligned?


Solution

  • Use this, using relative layout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true" >
    
            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp" />
    
            <TextView
                android:id="@+id/tv_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tv_name"
                android:layout_marginLeft="10dp" />
        </RelativeLayout>
    
        <RelativeLayout
            android:id="@+id/rl_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" >
    
            <Button
                android:id="@+id/btn_delete"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/btn_delete_new"
                android:focusable="false" />
        </RelativeLayout>
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_toLeftOf="@+id/rl_btn" >
    
            <ImageView
                android:id="@+id/iv_image"
                android:layout_width="50dp"
                android:layout_height="40dp"
                android:background="@drawable/app_icon_17"
                android:contentDescription="@string/empty" />
        </RelativeLayout>
    
    </RelativeLayout>