-1

I have a TextView whose width should not exceed the ImageView above it. Both image and text are downloaded from server and I don't know their dimensions (can't make assumptions either). I went through the logic to wrap the text content using this SO post.

Here is my layout XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentLL"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout android:orientation="vertical"
        android:id="@+id/LL1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:width="0dp"
            android:text="This is a string whose width may or may not be more than the image downloaded" />
    </LinearLayout>

    <TextView android:background="@android:color/holo_red_dark"
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Second Text"/>
</LinearLayout>

With this code, the TextView at the end (text2) does not even show up. There are 2 solutions to this issue :

  1. Apply android:maxLines="5" to the text1. Problem with this approach is that Text1 view would always be 5 lines high (I understand 'lines' is not a unit of height, but that's what I see visually). So if the text content is just one word, there would be a big white space below. And then text2 shows up.
  2. Change topmost linear layout (parentLL) to RelativeLayout. text2 can then be used with alignBelow=LL1. This works as expected. But I cannot migrate the topmost view to RelativeLayout, because this view is from a library not in my control. I can only modify LL1 and it's children. Due to my code, other views below (like text2) are suffering (by not showing up).
  3. There is a third approach for setting the textview as a compound drawable on ImageView. I guess that might work (haven't tested), but my requirement is to show the TextView if image download has failed (which can be detected only after a while). So I need to have a TextView. Also, my LinearLayout LL1 can have other children too.

I would request for some help understanding :

  1. Why is my code not showing up the content below the textview 'text1'? With width=0 on textview it seems to set the height of the parent to be match_parent.
  2. How is RelativeLayout able to handle this smoothly ? Can I replicate any of that behavior in TextView's onMeasure ? Assume I have callbacks to detect image has been downloaded, and I can get image width also.
Community
  • 1
  • 1
dev
  • 11,071
  • 22
  • 74
  • 122

2 Answers2

1

I think what you are running into is a conflict of setting the width and height but not setting the layout weight, which is a key factor in how Linear Layouts work. If you add one more vertical LinearLayout in there and then move @id/text2 into it, you should be set. You'll need something like the following (obviously modified to your specs, this was just a quick test). Note my use of android:layout_weight,

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView3" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/textView2" />
    </LinearLayout>
</LinearLayout>

Which splits the screen in half vertically as shown in this picture,

Photo of resulting layout

Matt Brown
  • 288
  • 2
  • 7
  • I tried moving my text2 into a LinearLayout and added weights to each parent LL. But no luck. Just to be clear, I am not looking to split the screen in some proportions - I want one view below another. I don't think weights have any role there. – dev May 11 '16 at 02:37
  • If you see the linked SO post in my answer, you will find that in order to wrap the text content, width 0 is needed alongwith layout_width as match_parent. – dev May 11 '16 at 03:03
  • Oh ok. Well, I tinkered around with your layout some more. I discovered that the `android:width= "0dp"` parameter is letting that text view expand to show all the text instead of cutting it off (like the link said). If you omit that part, then not all the text in `text1` shows up. This is noticeable in a not very wide photo. Then, because your text in `text1` is so long, it is pushing `text2` off the screen. So I was thinking the weights in linear layout would guarantee `text2` some room on the screen. – Matt Brown May 11 '16 at 03:07
0

I had to wrap the TextView in a RelativeLayout, which was wrapped by a LinearLayout. Not happy with this solution, but this is the only way for now.

dev
  • 11,071
  • 22
  • 74
  • 122