ExpandableTextView 1.0.0

I’m happy to announce the first release of my new Android library:  Android-ExpandableTextView. The name says it all, it’s just a plain simple expandable TextView, nothing more nothing less. The reason why I created this little library, is because I wasn’t satisfied with the other solutions I found on the internet. I’ve put the thing on GitHub, and it’s free to use by anyone!

Demo

ExpandableTextView extends from TextView and is able to expand or collapse. No fancy buttons or other exotic features, the rest is up to you!

Add dependency

	repositories {
	    jcenter()
	}

or

	repositories {
	    mavenCentral()
	}

or

	repositories {
	    maven {
	        url "https://dl.bintray.com/blogcat/maven"
	    }
	}

Library dependency

	dependencies {
	    compile ('at.blogc:expandabletextview:1.0.0')
	}

Usage

Using the ExpandableTextView is very easy, it’s just a regular TextView with some extra functionality added to it. By defining the android:maxLines attribute, you can set the default number of lines for the TextView collapsed state.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <at.blogc.android.views.ExpandableTextView
        android:id="@+id/expandableTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lorem_ipsum"
        android:maxLines="5"
        android:ellipsize="end"
        app:animation_duration="1000"/>

	<!-- Optional parameter animation_duration: sets the duration of the expand animation -->

    <Button
        android:id="@+id/button_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/expand"/>

</LinearLayout>

In your Activity or Fragment:

final ExpandableTextView expandableTextView = (ExpandableTextView) this.findViewById(R.id.expandableTextView);
final Button buttonToggle = (Button) this.findViewById(R.id.button_toggle);

// set animation duration via code, but preferable in your layout files by using the animation_duration attribute
expandableTextView.setAnimationDuration(1000L);

// toggle the ExpandableTextView
buttonToggle.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(final View v)
    {
        expandableTextView.toggle();
        buttonToggle.setText(expandableTextView.isExpanded() ? R.string.collapse : R.string.expand);
    }
});

// but, you can also do the checks yourself
buttonToggle.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(final View v)
    {
        if (expandableTextView.isExpanded())
        {
            expandableTextView.collapse();
            buttonToggle.setText(R.string.expand);
        }
        else
        {
            expandableTextView.expand();
            buttonToggle.setText(R.string.collapse);
        }
    }
});

// listen for expand / collapse events
expandableTextView.setOnExpandListener(new ExpandableTextView.OnExpandListener()
{
    @Override
    public void onExpand(final ExpandableTextView view)
    {
        Log.d(TAG, "ExpandableTextView expanded");
    }

    @Override
    public void onCollapse(final ExpandableTextView view)
    {
        Log.d(TAG, "ExpandableTextView collapsed");
    }
});