Friday, October 28, 2016

android - TextView ellipsize example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#b5c9d1"
    >
    <!--
        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.

                Constant : Value
                    none : 0
                    start : 1
                    middle : 2
                    end : 3
                    marquee : 4
    -->
    <!--
        android:maxLines
            Makes the TextView be at most this many lines tall. When used on an editable text,
            the inputType attribute's 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.
    -->
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Red, Green, Yellow, Olive, Black, Magenta, Pink, Orange, White"
        android:textSize="35sp"
        android:textColor="#ff0008"
        android:background="#f4ededc2"
        android:fontFamily="sans-serif-condensed"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:maxLines="2"
        android:ellipsize="end"
        />
</android.support.design.widget.CoordinatorLayout>

2. TextView ellipsize programmatically

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff2088"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Red, Green, Yellow, Olive, Black, Magenta, Pink, Orange, White"
        android:textSize="35sp"
        android:textColor="#134615"
        android:background="#f4e0ffe7"
        android:fontFamily="sans-serif-condensed"
        android:layout_margin="10dp"
        android:padding="10dp"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

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


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private TextView mTextView;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mTextView = (TextView) findViewById(R.id.tv);

        /*
            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
        */

        // Set the text view maximum lines
        mTextView.setMaxLines(2);

        /*
            void setEllipsize (TextUtils.TruncateAt where)
                Causes words in the text that are longer than the view's width 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).

                Parameters
                    where : TextUtils.TruncateAt
        */

        // Set the text view ellipsize programmatically
        mTextView.setEllipsize(TextUtils.TruncateAt.END);
    }
}