Monday, March 14, 2016

android - How to ellipse TextView programmatically

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="#adbeb4"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#227aed"
        android:background="#ecf1ff"
        android:text="This is a sample TextView to test ellipse"
        />
    <TextView
        android:id="@+id/tv_second"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        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 a sample TextView to test ellipse"
        />
    <TextView
        android:id="@+id/tv_third"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#219658"
        android:background="#f7ecff"
        android:layout_below="@id/tv_second"
        android:text="This is a sample TextView to test ellipse"
        />
    <TextView
        android:id="@+id/tv_fourth"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#2772b3"
        android:background="#f7ecff"
        android:layout_below="@id/tv_third"
        android:text="This is a sample TextView to test ellipse"
        />
    <TextView
        android:id="@+id/tv_fifth"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="18dp"
        android:layout_margin="5dp"
        android:padding="5dp"
        android:fontFamily="sans-serif-condensed"
        android:textColor="#b3277b"
        android:background="#f9fff2"
        android:layout_below="@id/tv_fourth"
        android:text="This is a sample\nTextView to test multiline\nellipse"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private TextView mTextViewSecond;
    private TextView mTextViewThird;
    private TextView mTextViewFourth;
    private TextView mTextViewFifth;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mTextViewSecond = (TextView) findViewById(R.id.tv_second);
        mTextViewThird = (TextView) findViewById(R.id.tv_third);
        mTextViewFourth = (TextView) findViewById(R.id.tv_fourth);
        mTextViewFifth = (TextView) findViewById(R.id.tv_fifth);

        /*
            public void setSingleLine (boolean singleLine)
                If true, sets the properties of this field (number of lines, horizontally scrolling,
                transformation method) to be for a single-line input; if false, restores these to
                the default conditions. Note that the default conditions are not necessarily those
                that were in effect prior this method, and you may want to reset these properties
                to your custom values.

            Parameters
                singleLine : boolean
        */
        /*
            public void setEllipsize (TextUtils.TruncateAt where)
                Causes words in the text that are longer than the view is wide to be ellipsized
                instead of broken in the middle. You may also want to setSingleLine() or
                setHorizontallyScrolling(boolean) to constrain the text to a single line. Use
                null to turn off ellipsizing. If setMaxLines(int) has been used to set two or
                more lines, only END and MARQUEE are supported (other ellipsizing types will
                not do anything).
        */
        /*
            TextUtils.TruncateAt
                Enum Values
                    END
                    MARQUEE
                    MIDDLE
                    START
        */
        // Display ellipse for TextView at the start
        mTextViewSecond.setSingleLine(true);
        mTextViewSecond.setEllipsize(TextUtils.TruncateAt.START);

        // Display ellipse for TextView at the middle
        mTextViewThird.setSingleLine(true);
        mTextViewThird.setEllipsize(TextUtils.TruncateAt.MIDDLE);

        // Display ellipse for TextView at the end
        mTextViewFourth.setSingleLine(true);
        mTextViewFourth.setEllipsize(TextUtils.TruncateAt.END);

        /*
            public void setMaxLines (int maxlines)
                Makes the TextView at most this many lines tall. Setting this value
                overrides any other (maximum) height setting.

            Parameters
                maxlines : int
        */
        // Display ellipse for multiline TextView
        mTextViewFifth.setMaxLines(2);
        mTextViewFifth.setEllipsize(TextUtils.TruncateAt.END);
    }
}