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
.
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.
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'
}
- Define the
etv_collapsedLines
xml attribute (setCollapsedLines(int lines)
method in Java orcollapsedLines
property in Kotlin) to set the number of lines in collapsed state. - 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:
<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() }
}
- 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-onlystate
property. Documentation of possible states. The state will be also automatically updated every timetext
,collapsedLines
orexpandedLines
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
}
Take a look at the library documentation with description of public functions and properties: http://arslancharyev31.github.io/Anko-ExpandableTextView
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 |
- Library overrides
android:ellipsize
attribute toTruncateAt.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. UsecollapsedLines
orexpandedLines
instead. - Library extensively overrides
android:layout_height
internally, therefore this attribute shouldn't be used. You can set it towrap_content
in the layout editor. - For quite obvious reasons,
collapsedLines
cannot be greater thanexpandedLines
and vice versa -expandedLines
cannot be less thancollapsedLines
. AnIllegalArgumentException
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.