0

I have a layout where I have a two TextViews horizontally next to each other. What I am trying to achieve is a view where the second TextView is aligned to the right of the first, but if the TextView in the first gets too long it will truncate when the right edge of the right TextView hits the edge of the container. The text is set dynamically and can be of varying lengths.

A diagram of what I am trying to achieve is below (yellow = container, red box = second TextView):diagram

The code I have at the moment is below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="@android:color/white"
        android:textIsSelectable="false"
        android:text="Test"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@id/textView1"
        android:background="@drawable/textView2Background"
        android:paddingBottom="2dp"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:paddingTop="2dp"
        android:text="TextView2"
        android:textColor="@android:color/white"
        android:textSize="15sp" />

</RelativeLayout>

However, the first TextView is only truncating when it hits the end of the container, at which point the second TextView has been pushed outside it and is no longer visible.

Thanks

Michael
  • 1,194
  • 2
  • 10
  • 19

2 Answers2

0

You could use an ImageSpan, check out this previous question:

SpannableString with Image example

Community
  • 1
  • 1
AndroidEnthusiast
  • 6,557
  • 10
  • 42
  • 56
0

I was able to solve this using the following layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="@android:color/white"
        android:textIsSelectable="false"
        android:textSize="18sp" />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/textView2Background"
            android:paddingBottom="2dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:paddingTop="2dp"
            android:textColor="@android:color/white"
            android:textSize="15sp" />
    </FrameLayout>

</LinearLayout>
Michael
  • 1,194
  • 2
  • 10
  • 19