Anko-ExpandableTextView
Anko-ExpandableTextView copied to clipboard
An expandable Android TextView written in Kotlin
Anko-ExpandableTextView
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
- Getting started
- Usage
- Supported features
- Extensions
- Documentation
- Useful xml attributes
- Important notes
- Contributing
- License
- Acknowledgments
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 gradle.build:
dependencies {
implementation 'tm.charlie.androidlib:expandable-textview:$LatestVersion'
}
Usage
- Define the
etv_collapsedLinesxml attribute (setCollapsedLines(int lines)method in Java orcollapsedLinesproperty in Kotlin) to set the number of lines in collapsed state. - Provide unique
idso that library could restore its state after configuration change.
Then use ExpandableTextView just as you would use any other TextView.
Xml snippet:
<tm.charlie.expandabletextview.ExpandableTextView
android:id="@+id/expandable_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_black"
android:text="@string/lipsum"
app:etv_collapsedLines="3"
app:etv_animationDuration="200"/>
Java snippet:
ExpandableTextView expandableTextView = findViewById(R.id.expandable_textview);
expandableTextView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// Make this ExpandableTextView expand/collapse on click event
((ExpandableTextView) v).toggle();
}
});
Kotlin snippet:
expandableTextView(text = lipsum) {
id = R.id.expandable_textview
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
ExpandableTextViewvia read-onlystateproperty. Documentation of possible states. The state will be also automatically updated every timetext,collapsedLinesorexpandedLinesproperties are changed. ExpandableTextViewpreserves expanded/collapsed state on configuration change, e.g. orientation change, if unique id is provided.
Extensions
Additionally, library provides extension function for simple DSL layout building, like so:
expandableTextView(text = "Lorem ipsum...") {
collapsedLines = 3
}
More in demo project.
Documentation
Take a look at the library documentation with description of public functions and properties: http://arslancharyev31.github.io/Anko-ExpandableTextView
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:ellipsizeattribute toTruncateAt.ENDin order to ensure correct behaviour, therefore setting this attribute either via xml or programmatically will have no effect. - Library extensively uses
android:maxLinesinternally, therefore this attribute shouldn't be used. UsecollapsedLinesorexpandedLinesinstead. - Library extensively overrides
android:layout_heightinternally, therefore this attribute shouldn't be used. You can set it towrap_contentin the layout editor. - For quite obvious reasons,
collapsedLinescannot be greater thanexpandedLinesand vice versa -expandedLinescannot be less thancollapsedLines. AnIllegalArgumentExceptionwill be thrown if either of these rules is violated.
Contributing
If you wish to send a pull request, please make sure to checkout from develop branch and merge with develop branch as well.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
This library was based on its Java counterpart: Android-ExpandableTextView.