Git Product home page Git Product logo

expandabletextview's Introduction

ExpandableTextView

Light-weighted, convenient implementation of expandable text view that supports expanding & collapsing animations for Android projects.

Why ExpandableTextView?

When the text is too long, a designer reasonably asks if it is possible to truncate the text by having a certain line count limitation. In addition, an expanding call-to-action (ie. "Read more") should be shown at the end of the text. When user taps the text, it expands to show the full content. ExpandableTextView helps creating such behaviour easily.

Demonstration

Normal RTL With drawable
normal.mp4
rtl.mp4
drawable.mp4
maxLines when expand Width changes at runtime
maxLines.mp4
width.mp4

Install

Step 1. Add the JitPack repository to your build file

  • AGP older than 7.1.0
// build.gradle (root level)
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  • AGP 7.1.0 and newer
// settings.gradle
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

// build.gradle (module level)
dependencies {
    implementation 'com.github.giangpham96:ExpandableTextView:1.0.2'
}

Usage

Example XML

    <io.github.giangpham96.expandabletextview.ExpandableTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/purple_100"
        android:padding="16dp"
        android:maxLines="10"
        app:expandAction="More"
        app:limitedMaxLines="2"
        app:expandActionColor="@color/blue_500"
        app:originalText="@string/long_text" />

Attributes

Attributes Type Description Default value
app:limitedMaxLines Int The maximum line counts when the text is collapsed 3
app:expandAction String The action at the end of truncated text such as "View more" "" (Nothing will be shown at the end of truncated text)
app:expandActionColor Color The color of expand action #ffaa66cc
app:originalText String The text to be displayed on this text view ""

Public functions

  • limitedMaxLines public getter & setter for the maximum line counts when the text is collapsed.
  • expandAction public getter & setter for the action at the end of truncated text such as "View more".
  • expandActionColor public getter & setter for the color of expand action.
  • originalText public getter & setter for the text to be displayed on this text view.
  • collapsed public getter to determine if the text is being collapsed
  • expanded public getter to determine if the text is being expanded
  • toggle function that makes the text changes its state from collapsed to expanded & vice versa. It also adds animation transition during the state change.

Notes

  • DO NOT directly use android:text or setText in this view. Use app:originalText or originalText instead. Attempting to use android:text or setText will lead to unexpected behaviour.
  • At any time, limitedMaxLines MUST always be less than or equal to maxLines. Otherwise, an exception will be thrown.
  • This view only supports TextUtils.TruncateAt.END.

expandabletextview's People

Contributors

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