Trim TextView in the middle, keeping trailing characters

I am struggling with the problem of overlapping text. I have two text comments side by side

asd1234 | september 1

If the left text image is added with a large number of characters ("567"), the result will be

asd…23{lost chars} | september 1

Desired Result:

asd…567 | september 1

My textview attributes

        android:ellipsize="middle"
        android:maxLines="1"
        android:scrollHorizontally="true"

Is there a way to make it work with attributes, or do I need to trim the string programmatically? What is the right way to do this? Thanks

EDIT

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_dark"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/header"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/date"
        android:layout_centerVertical="true"
        android:maxLines="1"
        android:textColor="@android:color/holo_blue_dark" />

    <TextView
        android:id="@+id/date"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:maxLines="1"
        android:textColor="@android:color/holo_blue_dark" />

</RelativeLayout>
+4
source share
1 answer

I made a few changes to your xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_dark"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/header"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/date"
        android:singleLine="true"
        android:text="I have wrote some longestttt text over here for testing the ellipsize"
        android:textColor="#fff" 
        android:ellipsize="middle"/>

    <TextView
        android:id="@+id/date"
        style="?android:attr/listSeparatorTextViewStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:maxLines="1"
        android:text="02-09-2014"
        android:textColor="#fff" />

</RelativeLayout>

Basically, exchanged maxLines = 1for singleLine = truewhat made the difference. Since before that I did not get the proper output:

This gives me a conclusion like:

enter image description here

Hope this helps.

+2
source

All Articles