Magnus
Magnus

Reputation: 18810

Make LinearLayout adjust it's height to a TextView with "wrap_content" height

I have a LinearLayout containing an ImageView with fixed height, and a TextView with wrap_content height. I want the LinearLayout to grow to fit the entire TextView if the text doesn't fit on one line.

My code:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="5dp"
        android:scaleType="fitCenter"
        android:src="@drawable/question_mark"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:singleLine="false"
        android:text="Some long text that doesnt fit on one line"/>
</LinearLayout>

The LinearLayout in the example above doesn't resize, so I only see "Some long text" and then the rest of the TextView is hidden.

Any way to get the LinearLayout to resize according to the TextView?

I know I can use RelativeLayout and other types of layouts to achieve this, but I'm asking about a solution using LinearLayout.

Upvotes: 2

Views: 2707

Answers (1)

Vidhi Dave
Vidhi Dave

Reputation: 5684

Try this :

enter image description here

<LinearLayout android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="5dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher_background"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Some long text that doesnt fit on one line"/>

</LinearLayout>

Upvotes: 1

Related Questions