Monday, March 14, 2016

android - How to display Ellipse in a 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="#c4b89f"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#227aed"
        android:background="#dce6ff"
        android:text="This is a sample TextView"
        />
    <!--
        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:singleLine
            Constrains the text to a single horizontally scrolling line instead of letting it wrap
            onto multiple lines, and advances focus instead of inserting a newline when you press
            the enter key. The default value is false (multi-line wrapped text mode) for
            non-editable text, but if you specify any value for inputType, the default is true
            (single-line input field mode).

            Must be a boolean value, either "true" or "false".

            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 singleLine.
    -->
    <TextView
        android:id="@+id/tv_second"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#29b327"
        android:background="#e0ffdc"
        android:layout_below="@id/tv"
        android:text="This is a sample TextView"
        android:singleLine="true"
        android:ellipsize="end"
        />
    <TextView
        android:id="@+id/tv_third"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#ed4022"
        android:background="#ecf5f9"
        android:layout_below="@id/tv_second"
        android:text="This is a sample TextView"
        android:singleLine="true"
        android:ellipsize="start"
        />
    <TextView
        android:id="@+id/tv_fourth"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#2c22ed"
        android:background="#e8f7df"
        android:layout_below="@id/tv_third"
        android:text="This is a sample TextView"
        android:singleLine="true"
        android:ellipsize="middle"
        />
    <TextView
        android:id="@+id/tv_fifth"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#9c9955"
        android:background="#f7dcef"
        android:layout_below="@id/tv_fourth"
        android:text="This is a sample TextView"
        android:singleLine="true"
        android:ellipsize="marquee"
        />
</RelativeLayout>