Search code examples
androidandroid-layoutalignment

Android - Same Alignment - Layout


I am showing data in list view. I want all names in same alignment. I used weight because I do not want image to place most of screen. But with this way I cannot align images in same way. Here is how it looks. image of page

Here is my 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="match_parent"
android:orientation="horizontal">


<ImageView
    android:id="@+id/imageView"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:paddingLeft="20dp"
    android:scaleType="fitStart"
    app:srcCompat="@drawable/food_icon" />

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

    <TextView
        android:id="@+id/menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="0dp"
        android:text="TextView"
        android:textSize="18sp"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        >

        <TextView
            android:id="@+id/cost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/owner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="TextView" />

        </LinearLayout>
       </LinearLayout>

  </LinearLayout>

I need help. Thank you...


Solution

  • Try this

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="match_parent"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:paddingLeft="20dp"
            android:scaleType="fitStart"
            app:srcCompat="@drawable/food_icon" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/imageView"
            android:layout_weight="3"
            android:orientation="vertical"
            android:paddingLeft="0dp">
    
            <TextView
                android:id="@+id/menu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:gravity="left"
                android:paddingLeft="0dp"
                android:text="TextView"
                android:textSize="18sp"
                android:textStyle="bold" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="8dp"
                android:orientation="horizontal">
    
                <TextView
                    android:id="@+id/cost"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="TextView"
                    android:textSize="14sp" />
    
                <TextView
                    android:id="@+id/owner"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="TextView" />
            </LinearLayout>
        </LinearLayout>
    
    </RelativeLayout>