Search code examples
androidandroid-layoutandroid-linearlayout

Android LinearLayout Alignment to right


Pleae have a look at the following XML

i want image Aligning to the Right before text

please look to this image

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="6dp" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@drawable/rounded_corner"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_category"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:contentDescription="@string/app_name"
        android:padding="0dp"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="8dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt_allphotos_categty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:text=""
        android:textColor="@color/texthoverclr"
        android:gravity="right"
        android:textSize="20sp" />
</LinearLayout>

i want align the photo to right then text after it to right


Solution

  • The key is one LinearLayout with the gravity of the layout set to android:gravity="end"

    <?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="60dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:background="@color/black"
    android:gravity="end"
    android:orientation="horizontal">
    
    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:src="@drawable/trophy" />
    
    <TextView
        android:id="@+id/toastTextView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center_vertical"
        android:text="Hello"
        android:textColor="@color/white"
        android:textSize="18sp" />
    
    </LinearLayout>