7

I'm trying to make layout like this:

|---------------------------| |---------------------------|
| Title of text goes there  | | Title of text goes there  |
|---------------------------| |---------------------------|
| Image goes there, height  | | Image goes there, height  |
| can be different          | | can be different, very    |  
|---------------------------| | very, very different      |     
| Long, very long text goes | |---------------------------|
| here, and here, and here  | | Long, very long text goes |
| that should fill space    | | here, and here, and here  |
| lef after title and ima.. | | that should fill space ...|
|---------------------------| |---------------------------|

Imagine now that I have 5 LinearLayout like this situated on the screen, 2 upper and 3 lower, all uniformly. The problem is that my @Long, very long text doesn't ellipsize and fill the space available at the same time. When I set android:maxLines, it works, but I don't have the constant maxLines, because image and title height changes.

        <!-- item -->
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip"
        android:layout_weight=".3">

        <!-- item title -->    
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:id="@+id/item_3_title"
            android:textSize="22sp"
            android:textStyle="bold"/>

         <!-- item image -->  
        <RelativeLayout
            android:id="@+id/item_3_image_holder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical">

            <ImageView
                android:id="@+id/item_3_image"
                android:layout_width="wrap_content"
                android:layout_height="120dip"
                android:scaleType="centerCrop"
                android:padding="2dip" >
            </ImageView>

            <ProgressBar
                android:id="@+id/item_3_progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true" >
            </ProgressBar>

        </RelativeLayout>

        <!-- item text -->    
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:id="@+id/item_3_text"
            android:ellipsize="end"
            android:textSize="18sp"/>                   

    </LinearLayout>  
Roman
  • 2,079
  • 4
  • 35
  • 53
  • can you please provide image of what you want? – baldguy Mar 05 '13 at 13:27
  • what do you mean that text "should fill space left after title and image" meanwhile you're using wrap_content in the parent layout? – karabara Mar 05 '13 at 13:28
  • I updated layout example that I want to achieve. Hope it is clear now. Layout height is constant, but title and image height inside it have different height, so I need to ellipsize multiline text and make it fill the space available – Roman Mar 05 '13 at 13:48

2 Answers2

12

Ok, finally found the answer here

ViewTreeObserver observer = textView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
    int maxLines = (int) textView.getHeight()
            / textView.getLineHeight();
    textView.setMaxLines(maxLines);
    textView.getViewTreeObserver().removeGlobalOnLayoutListener(
            this);
}
});
Community
  • 1
  • 1
Roman
  • 2,079
  • 4
  • 35
  • 53
  • 2
    Note that textView.getLineHeight() won't always give you what you would like because [markup within the text can cause individual lines to be taller or shorter than this height](http://developer.android.com/reference/android/widget/TextView.html#getLineHeight()). – yiati Jun 21 '13 at 16:13
  • @androiddeveloper Modify the line height if necessary https://stackoverflow.com/a/37754390/1229735 – yiati Feb 15 '18 at 22:16
  • @yiati How could this help? Some spans can be taller or shorter than text. Example is ImageSpan... – android developer Feb 15 '18 at 23:00
  • @androiddeveloper You just need to ensure that the spans cannot be taller. – yiati Feb 19 '18 at 03:15
  • @yiati First, I don't want to do that. I want to show them as they should, while truncating as needed. Second, I'm curious: how would you ensure the spans will be the exact same size? – android developer Feb 19 '18 at 08:11
  • @androiddeveloper You can not want to do it all you would like good luck – yiati Feb 19 '18 at 19:51
  • @yiati I don't understand the meaning of what you wrote. – android developer Feb 20 '18 at 17:09
  • "I don't want to do that", but that is your only option. Wanting it will not change it. – yiati Feb 21 '18 at 22:38
1
 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:padding="10dip"
    >

    <!-- item title -->    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:id="@+id/item_3_title"
        android:textSize="22sp"
        android:textStyle="bold"/>

     <!-- item image -->  
    <RelativeLayout
        android:id="@+id/item_3_image_holder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/item_3_title"
       >

        <ImageView
            android:id="@+id/item_3_image"
            android:layout_width="wrap_content"
            android:layout_height="120dip"
            android:scaleType="centerCrop"
            android:padding="2dip" >
        </ImageView>

        <ProgressBar
            android:id="@+id/item_3_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" >
        </ProgressBar>

    </RelativeLayout>

    <!-- item text -->    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:id="@+id/item_3_text"
        android:layout_below="@+id/item_3_image_holder"
        android:ellipsize="end"
        android:textSize="18sp"/>                   

</RelativeLayout>  

you need such kind of structure I hope you are able to interpret it.

Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37