Search code examples
androidtextviewandroid-linearlayout

How to make TextView be in only one row?


I have a list item, which contains two TextView in LinearLayout, vertical, one above another. The data of higher one is not static, I get it from the web and if it's too long, it start to fill new row and lower view disappears. So I want to make this first view fill only one row.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="@dimen/margin_small">

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

        <com..view.ContactView
            android:id="@+id/contact_icon"
            style="@style/ContactListInitialsText"
            android:layout_width="@dimen/contact_favorite_icon_size"
            android:layout_height="@dimen/contact_favorite_icon_size"
            app:request_photo="true"
            app:show_contact_presence="true" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginStart="@dimen/margin_small"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <TextView
                android:id="@+id/contact_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:textColor="@color/search_result_text_color"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/contact_status"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:textSize="12sp" />
        </LinearLayout>

        <CheckBox
            android:id="@+id/btn_star"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:button="@drawable/btn_star_select"
            android:paddingEnd="@dimen/margin_small" />
    </LinearLayout>

</RelativeLayout>

Solution

  • A nice solution would be to set these properties:

    android:ellipsize="end" 
    android:maxLines="1"
    

    Maxlines = 1, will not allow the textview to expand; Ellipsize = adds 3 dots to the end, showing there is more hidden text.