-1

There is my code below. I need obtain result like on this snipset:

When is short string:

1 line:  ABC (123)

When is long string:

1 line:  ABC ABC ABC ABC ABC...(123)

Any help?

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/id1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:ellipsize="end"
        android:maxLines="1"
        android:visibility="visible"
        android:text="ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC"
        android:textSize="@dimen/text_size_form"/>

    <TextView
        android:id="@+id/id2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="123"
        android:textSize="@dimen/text_size_form"/>

</LinearLayout>
waclaw
  • 433
  • 1
  • 7
  • 18

6 Answers6

2

You can also use RelativeLayout and property like below..

  1. alignParentRight for second TextView.
  2. First TextView toLeftOf of Second TextView.

Here is sample code.. Try this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/id1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/id2"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC"
        android:textStyle="bold"
        android:visibility="visible" />

    <TextView
        android:id="@+id/id2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="123"
        android:visibility="visible" />

</RelativeLayout>

Output is like below images.

enter image description here

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
1

Add this property android:singleLine="true" in your Textview

Jatin
  • 1,650
  • 3
  • 17
  • 32
1

You can check the String length, if its longer than a specific length, than you can display it like you want.

ex. max length you want is 20

if (str.length()>20) {
    str = str.substring(0, 20)+"..."; 
}
Neal Ahluvalia
  • 1,538
  • 1
  • 10
  • 20
0

Use below code

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/id1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textStyle="bold"
        android:ellipsize="end"
        android:maxLines="1"
        android:visibility="visible"
        android:text="ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC ABC"
        android:textSize="@dimen/text_size_form"/>

    <TextView
        android:id="@+id/id2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="visible"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="123"
        android:layout_marginLeft="-3dp"
        android:textSize="@dimen/text_size_form"/>

</LinearLayout>
Phuc Tran
  • 7,555
  • 1
  • 39
  • 27
0

Try-

    String s = "ABC ABC ABC ABC";

        if(s.split(" ").length>1)
        {
            s = s.split(" ")[0]+" ("+s.split(" ").length+")";
        }

textView.setText(s);
NehaK
  • 2,639
  • 1
  • 15
  • 31
0

just add this in your xml

android:ellipsize="middle"
android:singleLine="true"
justDroid
  • 343
  • 2
  • 9