Search code examples
androidandroid-linearlayouttextviewandroid-textattributes

Crop textView in LinearLayout


I have LinearLayout with two TextView's in it - textName and textPrice. Properties of the textName:

android:layout_width="wrap_content"
android:layout_height="wrap_content"

and properties of the textPrice:

android:layout_width="fill_parent"
android:layout_height="wrap_content"

In the case when the textName is long, it stretches over the entire line and closes a textPrice.

Here is my xml-markup:

<LinearLayout
        android:id="@+id/textNameTimeHolder"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail" android:layout_alignBottom="@+id/thumbnail">
    <TextView
            android:id="@+id/textName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Неудержимые 3"
            android:textColor="#040404"
            android:typeface="sans"
            android:textSize="15sp"
            android:textStyle="bold"
            android:singleLine="true"
            android:layout_toLeftOf="@+id/textPrice"/>
    <TextView
            android:id="@+id/textPrice"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="15sp"
            android:text="150 - 220 сом"
            android:layout_marginRight="5dp"
            android:layout_gravity="right"/>
</LinearLayout>

How can I crop a textName (using singleline), when it's too long?

Thanks.


Solution

  • set ellipsize property in your TextView and its better to use a weight in both TextView

    <LinearLayout
            android:id="@+id/textNameTimeHolder"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/thumbnail"
            android:layout_toRightOf="@+id/thumbnail"
            android:layout_alignBottom="@+id/thumbnail">
        <TextView
                android:id="@+id/textName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ellipsize="end"
                android:text="Неудержимые 3"
                android:textColor="#040404"
                android:typeface="sans"
                android:textSize="15sp"
                android:textStyle="bold"
                android:singleLine="true"/>
        <TextView
                android:id="@+id/textPrice"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ellipsize="end"
                android:gravity="right"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="15sp"
                android:text="150 - 220 сом"
                android:layout_marginRight="5dp"
                android:layout_gravity="right"/>
    </LinearLayout>