Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does TextView in single line elipsized with "end" show boxes?

I'm using a TextView in Android, what I want to show 1 line in TextView ending with ". " but this give [] type box at the end. I don't know why? I just want to remvoe this box and only to show text ending with "... "

TextView shown box at end

Update code for the list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="85dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:cacheColorHint="#4C7B8D"
android:background="#4C7B8D">
<ImageView
    android:id="@+id/videoListImage"
    android:src="@drawable/audio_thumbnail60x60"
    android:layout_height="75dp"
    android:layout_alignParentLeft="true"
    android:layout_centerInParent="true"
    android:scaleType="fitXY"
    android:layout_width="75dp"
    android:padding="4dp"
    android:background="@color/light_gray" />
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/next_arrow"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:paddingLeft = "5dp">
        <TextView
            android:id="@+id/row_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/app_background_color"
            android:textSize="18sp"
            android:ellipsize="end"             
            android:maxLines="1" />
        <TextView
            android:id="@+id/row_dis"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/color_black"
            android:layout_marginRight="2dp"
            android:textSize="15sp"
            android:ellipsize="end"
            android:maxLines="1" />
        <TextView
            android:text="$7.50"
            android:id="@+id/audio_price_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:textColor="@color/color_white"
            android:textStyle = "bold"
            android:paddingLeft = "12dp"
            android:paddingRight = "12dp"
            android:background="@drawable/blue_round_cornor_background" />
    </LinearLayout>
    <ImageView
        android:id="@+id/next_arrow"
        android:src="@drawable/next_arrow"
        android:layout_toLeftOf="@+id/saved_purchased"
        android:scaleType="fitXY"
        android:layout_height="25dp"
        android:layout_width="18dp"
        android:layout_centerVertical="true"
        android:visibility = "gone"/>
    <ImageView
        android:id="@+id/saved_purchased"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop ="true"
        android:scaleType="fitXY"
        android:layout_height="25dp"
        android:layout_width="25dp"
        android:layout_marginRight="2dp"/>
</RelativeLayout>

Here is the images of "next_arrow" enter image description here

Here is the code I am using the getView() in adapter.

  String discription = listData.getDescription();
    if (discription != null && discription.length() > 0) {
        if (textViewDis != null) {
            textViewDis.setTypeface(titleFont);
            Log.e("data", ""+discription);
            discription.replaceAll("\r\n", "");
            textViewDis.setText(discription);
        }
    }

Here is the actual String of description to be display.

Andrew and Stephanie Tidwell candidly share their success story in this business. This story will help everyone listening realize that no one is perfect, even in a second generation business. This is a streaming audio file.

Still have some issue? I can update question more.

like image 619
Arslan Anwar Avatar asked Jun 27 '11 11:06

Arslan Anwar


People also ask

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

How do you get to the next line in TextView?

for the new line in TextView just add \n in middle of your text it works..

How do I limit characters in TextView?

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.


2 Answers

Quoting myself from one of my books:

Android's TextView class has the built-in ability to "ellipsize" text, truncating it and adding an ellipsis if the text is longer than the available space. You can use this via the android:ellipsize attribute, for example. This works fairly well, at least for single-line text. The ellipsis that Android uses is not three periods. Rather it uses an actual ellipsis character, where the three dots are contained in a single glyph. Hence, any font that you use that you also use the "ellipsizing" feature will need the ellipsis glyph.

Beyond that, though, Android pads out the string that gets rendered on-screen, such that the length (in characters) is the same before and after "ellipsizing". To make this work, Android replaces one character with the ellipsis, and replaces all other removed characters with the Unicode character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF). This means the "extra" characters after the ellipsis do not take up any visible space on screen, yet they can be part of the string.

However, this means any custom fonts you use for TextView widgets that you use with android:ellipsize must also support this special Unicode character. Not all fonts do, and you will get artifacts in the on-screen representation of your shortened strings if your font lacks this character (e.g., rogue X's appear at the end of the line).

like image 79
CommonsWare Avatar answered Sep 21 '22 07:09

CommonsWare


I have bumped into the same problem when was trying to use custom "MetaPro-Medium.otf" as font for TextView with a android:singleLine="true". The box at the end of the string was really annoying.

I found no way how to solve this problem in Android, but at the same time found following work around.

  1. I have installed "FontLab Studio v5.04"
  2. Opened my font
  3. Selected one of symbols I was not going to use
  4. Menu->Glyph->Rename Glyph
  5. Changed the name and unicode index from it`s original value to "FEFF" (Thank CommonsWare)
  6. Double click on selected symbol and then remove all lines this symbol was created from.
  7. Menu->File->Generate Font->save as type otf

As a result I got updated font and problem gone away.

like image 34
Sergey Balashevich Avatar answered Sep 20 '22 07:09

Sergey Balashevich