Introduction: An expandable Android TextView written in Kotlin
More: Author   ReportBugs   

Download Preview the app License: MIT

An Expandable TextView for Android (Api 16+) which is entirely written in Kotlin and takes advantage of Anko. The library also handles configuration changes, so that the view remains expanded/collapsed on configuration change. Extends from AppCompatTextView.

Table of Contents

Demo Project

Take a look at the demo project with examples of using this library in Kotlin with Anko DSL as well as in Java with traditional xml.


Getting Started

The library is included in jCenter, so just add this dependency to your module level

dependencies {
    implementation 'tm.charlie.androidlib:expandable-textview:$LatestVersion'

Current latest version is: Download


  1. Define the etv_collapsedLines xml attribute (setCollapsedLines(int lines) method in Java or collapsedLines property in Kotlin) to set the number of lines in collapsed state.
  2. Provide unique id so that library could restore its state after configuration change.

Then use ExpandableTextView just as you would use any other TextView.

Xml snippet:


Java snippet:

ExpandableTextView expandableTextView = findViewById(;
expandableTextView.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        // Make this ExpandableTextView expand/collapse on click event
        ((ExpandableTextView) v).toggle();

Kotlin snippet:

expandableTextView(text = lipsum) {
    id =
    typeface = ResourcesCompat.getFont(context, R.font.lato_black)
    collapsedLines = 3
    animationDuration = 200

    // State change listener
    onStateChange { oldState, newState -> toast("$oldState -> $newState") }

    // Make ExpandableTextView expand/collapse on click event
    onClick { toggle() }

Supported features

  • Setting maximum number of collapsed lines and maxim number of expanded lines via both xml and Kotlin/Java.
  • Tracking the state of ExpandableTextView via read-only state property. Documentation of possible states. The state will be also automatically updated every time text, collapsedLines or expandedLines properties are changed.
  • ExpandableTextView preserves expanded/collapsed state on configuration change, e.g. orientation change, if unique id is provided.


Additionally, library provides extension function for simple DSL layout building, like so:

expandableTextView(text = "Lorem ipsum...") {
    collapsedLines = 3

More in demo project.


Take a look at the library documentation with description of public functions and properties:

Useful xml attributes

You can use ExpandableTextView in xml layouts in the same way as you would TextView. The library provides following attributes in addition to the ones defined in TextView.

Attribute name Format Description Default
etv_animationDuration integer >= 0 Duration of expand/collapse animation in milliseconds. 300
etv_collapsedLines integer >= 0 Number of lines in collapsed state. Must not be greater than etv_expandedLines. Integer.MAX_VALUE
etv_expandedLines integer >= 0 Number of lines in expanded state. Must not be less than etv_collapsedLines. Integer.MAX_VALUE

Important notes

  • Library overrides android:ellipsize attribute to TruncateAt.END in order to ensure correct behaviour, therefore setting this attribute either via xml or programmatically will have no effect.
  • Library extensively uses android:maxLines internally, therefore this attribute shouldn't be used. Use collapsedLines or expandedLines instead.
  • Library extensively overrides android:layout_height internally, therefore this attribute shouldn't be used. You can set it to wrap_content in the layout editor.
  • For quite obvious reasons, collapsedLines cannot be greater than expandedLines and vice versa - expandedLines cannot be less than collapsedLines. An IllegalArgumentException will be thrown if either of these rules is violated.


If you wish to send a pull request, please make sure to checkout from develop branch and merge with develop branch as well.


This project is licensed under the MIT License - see the LICENSE file for details.


This library was based on its Java counterpart: Android-ExpandableTextView.

Support Me
About Me
Google+: Trinea trinea
GitHub: Trinea