Giter Site home page Giter Site logo

android-expandabletextview's Introduction

Android-ExpandableTextView

An expandable TextView for Android applications (4.0+).

Download

Demo

This repository also contains a demo project.

Demo

Add dependency

This library is released in Maven Central and jCenter:

	repositories {
	    mavenCentral()
	}

or

	repositories {
	    jcenter()
	}

library dependency

	dependencies {
	    compile 'at.blogc:expandabletextview:1.0.3'
	}

Usage

Using the ExpandableTextView is very easy, it's just a regular TextView with some extra functionality added to it. By defining the android:maxLines attribute, you can set the default number of lines for the TextView collapsed state.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <at.blogc.android.views.ExpandableTextView
        android:id="@+id/expandableTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lorem_ipsum"
        android:maxLines="5"
        android:ellipsize="end"
        app:animation_duration="1000"/>

	<!-- Optional parameter animation_duration: sets the duration of the expand animation -->

    <Button
        android:id="@+id/button_toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/expand"/>

</LinearLayout>

In your Activity or Fragment:

final ExpandableTextView expandableTextView = (ExpandableTextView) this.findViewById(R.id.expandableTextView);
final Button buttonToggle = (Button) this.findViewById(R.id.button_toggle);

// set animation duration via code, but preferable in your layout files by using the animation_duration attribute
expandableTextView.setAnimationDuration(1000L);

 // set interpolators for both expanding and collapsing animations
expandableTextView.setInterpolator(new OvershootInterpolator());

// or set them separately
expandableTextView.setExpandInterpolator(new OvershootInterpolator());
expandableTextView.setCollapseInterpolator(new OvershootInterpolator());

// toggle the ExpandableTextView
buttonToggle.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(final View v)
    {
        expandableTextView.toggle();
        buttonToggle.setText(expandableTextView.isExpanded() ? R.string.collapse : R.string.expand);
    }
});

// but, you can also do the checks yourself
buttonToggle.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(final View v)
    {
        if (expandableTextView.isExpanded())
        {
            expandableTextView.collapse();
            buttonToggle.setText(R.string.expand);
        }
        else
        {
            expandableTextView.expand();
            buttonToggle.setText(R.string.collapse);
        }
    }
});

// listen for expand / collapse events
expandableTextView.setOnExpandListener(new ExpandableTextView.OnExpandListener()
{
    @Override
    public void onExpand(final ExpandableTextView view)
    {
        Log.d(TAG, "ExpandableTextView expanded");
    }

    @Override
    public void onCollapse(final ExpandableTextView view)
    {
        Log.d(TAG, "ExpandableTextView collapsed");
    }
});

Roadmap

  • add method to know if the TextView is expandable or not
  • optional fading edge at the bottom of the TextView
  • update demo project with more examples

License

Copyright 2016 Cliff Ophalvens (Blogc.at)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

android-expandabletextview's People

Contributors

cliffus avatar

Watchers

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