Giter Club home page Giter Club logo

expandabletextview's Introduction

!!! ANKO lib was deleted. Fix bug with deserialization. Downgrade min sdk version to API 15. Enjoy

ExpandableTextView

Download Preview the app License: MIT

An Expandable TextView for Android (Api 15+) 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.

Demo

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'
}

Current latest version is: Download

Usage

  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:

<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 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.

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: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.

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.

expandabletextview's People

Contributors

arslancharyev31 avatar josinsbazin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.