Monday, March 14, 2016

android - Ellipsize for multiline TextView

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#9fbac4"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#227aed"
        android:background="#ecf1ff"
        android:text="This is first line\nThis is second line\nThis is third line"
        />
    <!--
        android:ellipsize
            If set, causes words that are longer than the view is wide to be ellipsized instead
            of broken in the middle. You will often also want to set scrollHorizontally or
            singleLine as well so that the text as a whole is also constrained to a single line
            instead of still allowed to be broken onto multiple lines.

            Must be one of the following constant values.
                none
                start
                middle
                end
                marquee
    -->
    <!--
        android:maxLines
            Makes the TextView be at most this many lines tall. When used on an editable text, the
            inputType attribute value must be combined with the textMultiLine flag for the
            maxLines attribute to apply.

            Must be an integer value, such as "100".

            This may also be a reference to a resource (in the form "@[package:]type:name") or theme
            attribute (in the form "?[package:][type:]name") containing a value of this type.

            This corresponds to the global attribute resource symbol maxLines.
    -->
    <!--
        If android:maxLines has been used to set two or more lines, only END and MARQUEE are
        supported (other ellipsizing types will not do anything).
    -->
    <TextView
        android:id="@+id/tv_second"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#b3277b"
        android:background="#f7ecff"
        android:layout_below="@id/tv"
        android:text="This is first line\nThis is second line\nThis is third line"
        android:ellipsize="end"
        android:maxLines="2"
        />
</RelativeLayout>