I have a textview that I am programmatically setting maxLines with and then setting it to android:ellipsize="end". It works partially, but for some reason the text that is being truncated continues for a few characters after the ellipsis in 4.2.2. It is the actual text that is being truncated, not new characters.
It also is not working properly in 2.3.6 as it adds an ellipsis for each section with a line break or "\n".
I am not using any custom fonts, I have seen that has caused problems before.
Here is where I calculate and set the maxlines:
final TextView reviewView = (TextView) view.findViewById(R.id.textViewPosition3);
ViewTreeObserver observer = reviewView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int maxLines = (int) reviewView.getHeight() / reviewView.getLineHeight();
reviewView.setMaxLines(maxLines);
reviewView.setEllipsize(TruncateAt.END);
reviewView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//reviewView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentTestLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/restaurantImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="@drawable/test"
android:layout_marginTop="-10dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
/>
<ImageView
android:id="@+id/squareBlack"
android:layout_alignRight="@id/restaurantImage"
android:layout_alignTop="@id/restaurantImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/square_black_small"
/>
<TextView
android:id="@+id/textViewPosition"
android:layout_alignRight="@id/squareBlack"
android:layout_alignLeft="@id/squareBlack"
android:layout_alignBottom="@id/squareBlack"
android:layout_alignTop="@id/squareBlack"
android:layout_margin="1dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a test"
android:textSize="25sp"
android:textColor="#a6cbff"
android:textStyle="bold"
/>
<ImageView
android:id="@+id/squareBlue"
android:layout_toLeftOf="@id/squareBlack"
android:layout_alignTop="@id/squareBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/square_blue_small"
/>
<TextView
android:id="@+id/textViewPosition1"
android:layout_alignRight="@id/squareBlue"
android:layout_alignLeft="@id/squareBlue"
android:layout_alignBottom="@id/squareBlue"
android:layout_alignTop="@id/squareBlue"
android:layout_margin="1dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7.5"
android:textSize="25sp"
android:textColor="#902515"
android:textStyle="bold"
/>
<TextView
android:id="@+id/restaurantName"
android:layout_alignLeft="@id/restaurantImage"
android:layout_alignBottom="@id/restaurantImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restaurant Name"
android:textSize="25sp"
android:textColor="#fff"
android:textStyle="bold"
android:background="@drawable/greybar"
/>
<TextView
android:id="@+id/textViewPosition2"
android:layout_below="@id/restaurantImage"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="February 15, 2013"
android:textColor="#555"
android:textStyle="bold"
android:textSize="15sp"
/>
<Button android:id="@+id/readReview"
android:background="@drawable/readreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Read the Review"
android:textColor="#fff"
android:textColorHighlight="#ccc"
android:textSize="15sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/textViewPosition3"
android:layout_below="@id/textViewPosition2"
android:layout_above="@id/readReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColorLink="#C90404"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:textColor="#000"
/>
</RelativeLayout>
I solved this programatically, but I'm not sure it's the best solution as it feels like more of a hack. It seems to be working though.
ViewTreeObserver observer = reviewView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int maxLines = (int) reviewView.getHeight() / reviewView.getLineHeight();
reviewView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
if(reviewView.getLineCount() > maxLines)
{
int lineEndIndex = reviewView.getLayout().getLineEnd(maxLines - 1);
String text = reviewView.getText().subSequence(0, lineEndIndex-3) +" ...";
reviewView.setText(text);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With