nr1
nr1

Reputation: 777

LinearLayout: TextViews at left, big ImageView at right

i'm working on a LinearLayout but unfortunately it's not working as it should. The goal is to have a LinearLayout with two TextViews (one placed below the other) on the left side, and an ImageView on the right side.

The ImageView should be as big as possible, the TextViews should take the remaining space.

At the moment my layout XML is like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="1dp"
android:background="@drawable/background" >

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

    <TextView
        android:id="@+id/layout1label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:singleLine="true"
        android:text="1234"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/layout2label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1234_label2"
        android:textSize="14dp" />
</LinearLayout>


<ImageView
    android:id="@+id/layout_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_vertical"
    android:layout_margin="2dp"
    android:layout_weight="1"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_launcher" />

The part that isn't working: If the text in the TextViews is "too long", the ImageView gets shrinked. I want it exactly the other way round.

Any solutions?

Upvotes: 2

Views: 4280

Answers (3)

Bala Vishnu
Bala Vishnu

Reputation: 2628

A little modification to Kirans Code..worked for me

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/layout1label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:singleLine="true"
        android:text="Name: BalaVishnu"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</LinearLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <ImageView
        android:id="@+id/layout_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:adjustViewBounds="true"
        android:src="@drawable/edit_icon" />
</LinearLayout>

Upvotes: 0

Todd Sjolander
Todd Sjolander

Reputation: 1569

It would be more efficient to use RelativeLayout instead of LinearLayout. Then you can place your views without having to nest layouts:

<?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" >
  <TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/image"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    />
  <TextView
    android:id="@+id/subtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/image"
    android:layout_below="@+id/title"
    />
  <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    />
</RelativeLayout>

By arranging the TextViews to be relative to the ImageView instead of the other way around, the ImageView takes priority for the space, and the text works with the remainder.

Upvotes: 5

Kiran Android
Kiran Android

Reputation: 286

this might be help to you

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="1dp"
    android:background="@drawable/background" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="2dp"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/layout1label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:singleLine="true"
            android:text="1234"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/layout2label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1234_labj hairu iue rel2"
            android:textSize="14dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/layout_image"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center_vertical"
            android:layout_margin="2dp"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions