Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ellipsize does not work properly

I'm building a new InfoWindow (google maps api v2) and I'm trying to get my layout to be perfect. A part of that layout is this:

  <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
  android:id="@+id/txtInfoWindowName"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  android:ellipsize="end"
  android:singleLine="true"
  android:textColor="#ff000000"
  android:textSize="14dp"
  android:textStyle="bold"/>
<TextView
  android:id="@+id/txtInfoWindowObstacles"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:ellipsize="end"
  android:maxLength="32"
  android:lines="1"
  android:singleLine="true"
  android:textColor="#ff7f7f7f"
  android:textSize="14dp"/>

Now, the problem is with the android:ellipsize="end". It should draw the three dots at the end but it doesn't do that. Now I'm getting something like this:

TextTextTextTextTe

Instead of this:

TextTextTextTextTe...

I'm thinking it has something to do with the fact that I'm using layout_width="wrap_content" . But I need that because I'm using a LinearLayout

like image 923
dumazy Avatar asked Jun 16 '26 08:06

dumazy


2 Answers

I tried your code and they work fine, except the second TextView which has the attribute android:maxLength="32", which in this case the text cannot be ellipsized because of 32 characters limit. But if you remove it, it works as expected.

like image 96
Andy Res Avatar answered Jun 17 '26 21:06

Andy Res


There is another option - you can format your string from code like this:

private static final int STR_MAX_CHAR_COUNT = 32;

private String formatString(String stringToFormat) {
    if(stringToFormat.length() > STR_MAX_CHAR_COUNT){
        stringToFormat = stringToFormat.substring(0, STR_MAX_CHAR_COUNT - 1) + "...";
    }
    return stringToFormat;
}

And then just set string like this:

String newText = "some long-long text";
mTextView.setText(formatString(newText))
like image 32
vir us Avatar answered Jun 17 '26 22:06

vir us



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!