Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView - height to fill max space and ellipsize

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>  
like image 384
Roman Avatar asked Mar 05 '13 13:03

Roman


2 Answers

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);
}
});
like image 113
Roman Avatar answered Nov 06 '22 22:11

Roman


 <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.

like image 20
Aashish Bhatnagar Avatar answered Nov 06 '22 21:11

Aashish Bhatnagar