Jorge Gil - 'xɔɾ.xɛ xil Jorge Gil - 'xɔɾ.xɛ xil - 11 months ago 123
Android Question

Android - Expandable TextView with Animation

I have a

which firstly shows a small portion of a long text.

The user can press a "see more" button to expand the
and see the rest of that text.

Making tests, I can reach that by simply interchange the value of
between 4 for collapsing and Integer.MAX_VALUE for expanding.

Now, I would like that this behavior would be accompanied by an animation. I know that in this question one solution is almost done, but I tried to implement it and I have no success.

Someone can help me with this?

Thanks in advance.


You can check my blog post on ExpandableTexTView:

The idea is, initially the TextView will show a small portion of a long text and when it is clicked, it will show the rest of the text.

So here is the code that how I solved it.

package com.rokonoid.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
 * User: Bazlur Rahman Rokon
 * Date: 9/7/13 - 3:33 AM
public class ExpandableTextView extends TextView {
    private static final int DEFAULT_TRIM_LENGTH = 200;
    private static final String ELLIPSIS = ".....";

    private CharSequence originalText;
    private CharSequence trimmedText;
    private BufferType bufferType;
    private boolean trim = true;
    private int trimLength;

    public ExpandableTextView(Context context) {
        this(context, null);

    public ExpandableTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView);
        this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH);

        setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                trim = !trim;

    private void setText() {
        super.setText(getDisplayableText(), bufferType);

    private CharSequence getDisplayableText() {
        return trim ? trimmedText : originalText;

    public void setText(CharSequence text, BufferType type) {
        originalText = text;
        trimmedText = getTrimmedText(text);
        bufferType = type;

    private CharSequence getTrimmedText(CharSequence text) {
        if (originalText != null && originalText.length() > trimLength) {
            return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS);
        } else {
            return originalText;

    public CharSequence getOriginalText() {
        return originalText;

    public void setTrimLength(int trimLength) {
        this.trimLength = trimLength;
        trimmedText = getTrimmedText(originalText);

    public int getTrimLength() {
        return trimLength;

And add following line in your attr.xml

<?xml version="1.0" encoding="utf-8"?>
<declare-styleable name="ExpandableTextView">
<attr name="trimLength" format="integer"/>

Put the following in your main.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android=""

And test your activity

package com.rokonoid.widget;

import android.os.Bundle;

public class MyActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {

        String yourText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
                "Ut volutpat interdum interdum. Nulla laoreet lacus diam, vitae " +
                "sodales sapien commodo faucibus. Vestibulum et feugiat enim. Donec " +
                "semper mi et euismod tempor. Sed sodales eleifend mi id varius. Nam " +
                "et ornare enim, sit amet gravida sapien. Quisque gravida et enim vel " +
                "volutpat. Vivamus egestas ut felis a blandit. Vivamus fringilla " +
                "dignissim mollis. Maecenas imperdiet interdum hendrerit. Aliquam" +
                " dictum hendrerit ultrices. Ut vitae vestibulum dolor. Donec auctor ante" +
                " eget libero molestie porta. Nam tempor fringilla ultricies. Nam sem " +
                "lectus, feugiat eget ullamcorper vitae, ornare et sem. Fusce dapibus ipsum" +
                " sed laoreet suscipit. ";

        ExpandableTextView expandableTextView = (ExpandableTextView) findViewById(;


Reference: Android – Expandable TextView