Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set TextView width according to other View's width

I have a ImageView, the image is loaded from the net and can vary in width. Below I have a TextView with a description of the image. I want the TextView to be exactly as wide as the ImageView.

My try was this:

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />

            <TextView
                android:id="@+id/imagedescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:textAppearance="@style/SmallTextGray" />
        </LinearLayout>

which somehow works as the TextView's width is set accoring to that of the ImageView, but the TextView end after 2 lines:

enter image description here

I know I could set the TextView's width programmatically after I know the image's width, but is there also a way to do it in the XML layout?

like image 856
fweigl Avatar asked Dec 04 '25 03:12

fweigl


1 Answers

I don't know if it will work, but try doing in your code, after load the image:

textView.setWidth(imageView.getDrawable().getIntrinsicWidth());
like image 117
Renan Bandeira Avatar answered Dec 06 '25 17:12

Renan Bandeira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!