Search code examples
androidandroid-linearlayoutandroid-imageviewandroid-xml

Why does image view take lot of space than mentioned by weight?


My XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/rl_upload_header"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:minHeight="50dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/upload_activity"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <LinearLayout
        android:id="@+id/ll_middle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/ll_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center" >

            <ImageView
                android:id="@+id/image_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/app_icon_17"
                android:contentDescription="@string/empty" >
            </ImageView>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/ll_eds"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:baselineAligned="false"
            android:gravity="center"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/ed_img_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="20dp"
                android:background="@drawable/text_box_background"
                android:hint="@string/name"
                android:paddingLeft="12dp" />

            <EditText
                android:id="@+id/ed_amount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/text_box_background"
                android:hint="@string/amount"
                android:paddingLeft="12dp" />

            <EditText
                android:id="@+id/ed_desc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/text_box_background"
                android:hint="@string/desc"
                android:paddingLeft="12dp" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/rl_upload_bottom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:gravity="center"
    android:minHeight="70dp"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:baselineAligned="false"
        android:gravity="center" >

        <Button
            android:id="@+id/button_upload"
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:background="@drawable/upload" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:baselineAligned="false"
        android:gravity="center"
        android:minHeight="50dp" >

        <Button
            android:id="@+id/button_cancel"
            android:layout_width="120dp"
            android:layout_height="40dp"
            android:background="@drawable/discard" >
        </Button>
    </LinearLayout>
</LinearLayout>

</LinearLayout>

In this there is ImageVIew(id =image_view) which I want to take only 25% approx space (width) of screen and remaining width should be taken by layout containing edit texts (id =ll_eds).

But when the image is big in width, its taking almost 90% width of screen.

So what wrong I'm doing?

Roughly I want this kind of design.


Solution

  • Set layout_width to 0 dip when using layout_weight on a View.

    See Android Engineer Romain Guy's answer: Android Layout Weight