A Custom TextView with trim text

Overview

ReadMoreTextView

A Custom TextView with trim text

Download

To add the ReadMoreTextView library to your Android Studio project, simply add the following gradle dependency:

compile 'com.borjabravo:readmoretextview:2.1.0'

Usage

To use the ReadMoreTextView on your app, add the following code to your layout:

<com.borjabravo.readmoretextview.ReadMoreTextView
    android:id="@+id/text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

You can customize ReadMoreTextView with:

  • app:trimExpandedText: Text that appears when the view is expanded.
  • app:trimCollapsedText: Text that appears when the view is collapsed.
  • app:trimLength: Trim length to determine when the clickable text is displayed.
  • app:showTrimExpandedText: Show the trim text if it's true.
  • app:colorClickableText: Text color of trim clickable text.
  • app:trimMode: Choose trim mode with two options: trimModeLength or trimModeLines.

Screenshots

screenshot screenshot

License

Copyright 2016 Borja Bravo

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.
Comments
  • StringIndexOutBounds when text is less than trim length

    StringIndexOutBounds when text is less than trim length

    If you try to trim by length, and the length of the text is smaller than the limit, you get

    java.lang.StringIndexOutOfBoundsException: length=8; regionStart=0; regionLength=101 at java.lang.String.startEndAndLength(String.java:298) at java.lang.String.getChars(String.java:598) at android.text.TextUtils.getChars(TextUtils.java:81) at android.text.SpannableStringBuilder.(SpannableStringBuilder.java:65) at com.borjabravo.readmoretextview.ReadMoreTextView.updateCollapsedText(ReadMoreTextView.java:137) at com.borjabravo.readmoretextview.ReadMoreTextView.getTrimmedText(ReadMoreTextView.java:115) at com.borjabravo.readmoretextview.ReadMoreTextView.getDisplayableText(ReadMoreTextView.java:92) at com.borjabravo.readmoretextview.ReadMoreTextView.setText(ReadMoreTextView.java:86)

    bug 
    opened by nilskassube 11
  • NPE

    NPE

    
    06-16 16:55:36.242 27838-27838/com W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getLineCount()' on a null object reference
    06-16 16:55:36.244 27838-27838/com W/System.err:     at com.borjabravo.readmoretextview.ReadMoreTextView.refreshLineEndIndex(ReadMoreTextView.java:219)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at com.borjabravo.readmoretextview.ReadMoreTextView.access$400(ReadMoreTextView.java:36)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at com.borjabravo.readmoretextview.ReadMoreTextView$1.onGlobalLayout(ReadMoreTextView.java:205)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:912)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1969)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at android.view.Choreographer.doFrame(Choreographer.java:606)
    06-16 16:55:36.244 27838-27838/com W/System.err:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    06-16 16:55:36.245 27838-27838/com W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
    06-16 16:55:36.245 27838-27838/com W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
    06-16 16:55:36.245 27838-27838/com W/System.err:     at android.os.Looper.loop(Looper.java:148)
    06-16 16:55:36.245 27838-27838/com W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
    06-16 16:55:36.245 27838-27838/com W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    06-16 16:55:36.245 27838-27838/com W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    06-16 16:55:36.245 27838-27838/com W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    
    

    Tested on device lollipop.

    opened by yolapop 4
  • App crushes with StringIndexOutOfBoundsException

    App crushes with StringIndexOutOfBoundsException

    This happens when text leght is shorter than field may fit without trimming and parameter app:trimMode="trimModeLine" is set.

    For example If I set attributes as following: app:trimMode="trimModeLine" app:trimLength="5"

    and try to set some short text : readmoretextview.setText("some text"); // lenght() = 9 which is shorter than 5 lines

    It will crush.

    Meanwhile attribute app:trimMode="trimModeLength" works correctly with any text lenghts

    opened by droidcrib 3
  • BUG

    BUG

    tv.setText(str); //无效

    修复: ReadMoreTextView.java

    @Override
    public void setText(CharSequence text, BufferType type) {
        this.text = text;
        bufferType = type;
        **onGlobalLayoutLineEndIndex();**
        setText();
    }
    
    opened by DuShuYuan 2
  • java.lang.IndexOutOfBoundsException: getChars (0 ... 241) ends beyond length 13...

    java.lang.IndexOutOfBoundsException: getChars (0 ... 241) ends beyond length 13...

    I use ReadMoreTextView in RecyclerView adapter.

    -------------this crash log:

    java.lang.IndexOutOfBoundsException: getChars (0 ... 241) ends beyond length 13 at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1094) at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:976) at android.text.TextUtils.getChars(TextUtils.java:91) at android.text.SpannableStringBuilder.(SpannableStringBuilder.java:65) at com.borjabravo.readmoretextview.ReadMoreTextView.updateCollapsedText(ReadMoreTextView.java:137) at com.borjabravo.readmoretextview.ReadMoreTextView.getTrimmedText(ReadMoreTextView.java:115) at com.borjabravo.readmoretextview.ReadMoreTextView.getDisplayableText(ReadMoreTextView.java:92) at com.borjabravo.readmoretextview.ReadMoreTextView.setText(ReadMoreTextView.java:86) at com.borjabravo.readmoretextview.ReadMoreTextView.access$200(ReadMoreTextView.java:36) at com.borjabravo.readmoretextview.ReadMoreTextView$1.onGlobalLayout(ReadMoreTextView.java:206) at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:982) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2410) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920) at android.view.Choreographer.doCallbacks(Choreographer.java:695) at android.view.Choreographer.doFrame(Choreographer.java:631) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

    -----------and this is my code:

            SpannableString ss = new SpannableString(Html.fromHtml("<font color='#5376a0'> " + 
            userNamesSb + "</font><font color='#666666'> " + action + ":</font>" + msgDesc));
            
            ss.setSpan(noUnderlineSpan, 0, userNamesSb.toString().length(), 
                               Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
            
            readMoreTV.setHighlightColor(getResources().getColor(R.color.colorAccent));
            readMoreTV.setText(ss);
    

    please fix it, thank you!

    opened by scrylove 1
  • Can it use in CollapseLayout?

    Can it use in CollapseLayout?

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_large"
            android:fitsSystemWindows="false"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_horizontal_margin"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
    
                <include layout="@layout/profile_layout"/>
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    

    In profile_layout I add a ReadMoreTextView. But it doesn't collapse in AS preview or in actual device.

    opened by DanteAndroid 1
  • Cleanup and update build config

    Cleanup and update build config

    Cleaned up the build configuration a little. This also includes updates to the build tools and Android target versions.

    Replaced the general support dependency with the only one actually needed.

    Removed all auto generated tests from the code base.

    opened by tynn 1
  • about ReadMoreTextView

    about ReadMoreTextView

    I am sorry my English, I am chinese If We customize ReadMoreTextView with app:trimExpandedText="收起" in xml file, It cant response? When I try ReadMoreTextView .setTrimCollapsedText in java code, It can response

    opened by pudao2010 1
  • compile path wrong

    compile path wrong

    I find the compile path wrong should be compile 'com.borjabravo:readmoretextview:1.1.1' not be compile 'com.borjabravo.readmoretextview:1.1.1' the diffrent is : , not .

    opened by caoguodong 1
  • Failed to resolve:

    Failed to resolve:

    Hi guys,

    I tried to import your dependency, but, when I try to build on my :app, it appears:

    Could not find com.borjabravo:readmoretextview:2.1.0. Required by: project :app

    How can I fix it to implement?

    opened by clcmo 0
  • Getting crash due to StringIndexOutOfBoundsException inside RecyclerView

    Getting crash due to StringIndexOutOfBoundsException inside RecyclerView

    I'm using ReadMoreTextView in RecyclerView. Getting crash due to StringIndexOutOfBoundsException so many times inside RecyclerView.

    E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.waspito, PID: 17585
        java.lang.StringIndexOutOfBoundsException: length=35; index=241
            at java.lang.String.getChars(String.java:788)
            at android.text.TextUtils.getChars(TextUtils.java:148)
            at android.text.SpannableStringBuilder.<init>(SpannableStringBuilder.java:68)
            at com.borjabravo.readmoretextview.ReadMoreTextView.updateCollapsedText(ReadMoreTextView.java:137)
            at com.borjabravo.readmoretextview.ReadMoreTextView.getTrimmedText(ReadMoreTextView.java:114)
            at com.borjabravo.readmoretextview.ReadMoreTextView.getDisplayableText(ReadMoreTextView.java:90)
            at com.borjabravo.readmoretextview.ReadMoreTextView.setText(ReadMoreTextView.java:84)
            at com.borjabravo.readmoretextview.ReadMoreTextView.access$200(ReadMoreTextView.java:33)
            at com.borjabravo.readmoretextview.ReadMoreTextView$1.onGlobalLayout(ReadMoreTextView.java:206)
            at android.view.ViewTreeObserver.dispatchOnGlobalLayout(ViewTreeObserver.java:1056)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2629)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1722)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7605)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1029)
            at android.view.Choreographer.doCallbacks(Choreographer.java:852)
            at android.view.Choreographer.doFrame(Choreographer.java:787)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1014)
            at android.os.Handler.handleCallback(Handler.java:883)
            at android.os.Handler.dispatchMessage(Handler.java:100)
            at android.os.Looper.loop(Looper.java:214)
            at android.app.ActivityThread.main(ActivityThread.java:7397)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
    
    opened by kasim1011 10
  • conflict when there is setOnClickListener on the text

    conflict when there is setOnClickListener on the text

    my entire text has setOnClickListener action to call another activity, but I don't want this to happen when the user click on "read more" and "read less" part of text (I obviously simply want the text to expand and colapse). How to isolate this special parts of text to not be called on "ClickListener"?

    opened by XandiVieira 0
  • Fatal Exception: java.lang.NullPointerException

    Fatal Exception: java.lang.NullPointerException

    Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getLineCount()' on a null object reference at com.borjabravo.readmoretextview.ReadMoreTextView.getTrimmedText(ReadMoreTextView.java:113) at com.borjabravo.readmoretextview.ReadMoreTextView.getDisplayableText(ReadMoreTextView.java:90) at com.borjabravo.readmoretextview.ReadMoreTextView.setText(ReadMoreTextView.java:84) at com.borjabravo.readmoretextview.ReadMoreTextView.setText(ReadMoreTextView.java:97)

    opened by Sunaina015 0
Owner
Borja B.
Mobile Developer
Borja B.
Mentions-TextView - Make Mentions and hashtags clickable in Textview

Mentions Textview Custome Textview with Mentions and hashtags being clickable. D

null 2 Jan 9, 2022
Animation effects to text, not really textview

HTextView Animation effects with custom font support to TextView see iOS Effects see Flutter Effects Screenshot type gif Scale Evaporate Fall Line Typ

hanks 5.5k Jan 5, 2023
A TextView that automatically resizes text to fit perfectly within its bounds.

AutoFitTextView A TextView that automatically resizes text to fit perfectly within its bounds. Usage dependencies { compile 'me.grantland:autofitt

Grantland Chew 4.2k Jan 1, 2023
Simple way to create linked text, such as @username or #hashtag, in Android TextView and EditText

Simple Linkable Text Simple way to create link text, such as @username or #hashtag, in Android TextView and EditText Installation Gradle Add dependenc

Aditya Pradana Sugiarto 76 Nov 29, 2022
A editable text with a constant text/placeholder for Android.

ParkedTextView A EditText with a constant text in the end. How to use <com.goka.parkedtextview.ParkedTextView xmlns:app="http://schemas.android.co

goka 270 Nov 11, 2022
RTL marquee text view android right to left moving text - persian - farsi - arabic - urdo

RTL marquee text view android right to left moving text - persian - farsi - arabic - urdo

mehran elyasi 4 Feb 14, 2022
Android library contain custom realisation of EditText component for masking and formatting input text

Masked-Edittext Masked-Edittext android library EditText widget wrapper add masking and formatting input text functionality. Install Maven <dependency

Evgeny Safronov 600 Nov 29, 2022
Custom view to expand long text with view more,less action , you can customize min lines , action color

ExpandableTextView Custom expadable text view Examples : <mostafa.projects.expandabletextview.ExpandableTextView android:layout_wi

Mostafa Gad 8 Jan 25, 2022
Android Custom View for prevent the view behind on-screen keyboard when edit text is focused

Group Focusable Prevent the view behind on-screen keyboard when edit text is focused in Android UI Download Gradle implementation 'com.akexorcist:grou

Akexorcist 8 Jun 22, 2022
LocalizedEditText - Custom edit text that allow only one language

LocalizedEditText Custom edit text that allow only one language Supported languages : Arabic , English Default language : English Examples

Mostafa Gad 1 Nov 28, 2021
Android's TextView that can expand/collapse like the Google Play's app description

ExpandableTextView ExpandableTextView is an Android library that allows developers to easily create an TextView which can expand/collapse just like th

Manabu S. 4k Dec 28, 2022
A library to show emoji in TextView, EditText (like WhatsApp) for Android

Discontinued This projected is discontinued. Please consider using other alternative, i.e EmojiCompat. Contact me if you want to continue working on a

Hieu Rocker 3.6k Jan 5, 2023
Android experiment showing a sinking TextView

Titanic is an Android experiment reproducing this effect.

Romain Piel 1.8k Dec 15, 2022
Advanced Android TextView

Advanced Android TextView Companion app for my Advanced Android TextView talk, demostrating: Animated CompoundDrawable Text shadow Custom font Non-bre

Chiu-Ki Chan 1.2k Dec 9, 2022
AutoLinkTextView is TextView that supports Hashtags (#), Mentions (@) , URLs (http://), Phone and Email automatically detecting and ability to handle clicks.

AutoLinkTextView Deprecated Please use the new version of AutoLinkTextView AutoLinkTextView is TextView that supports Hashtags (#), Mentions (@) , URL

Arman 1.1k Nov 23, 2022
Implementation of a TextView and all its direct/indirect subclasses with native support for the Roboto fonts, includes the brand new Roboto Slab fonts.

Android-RobotoTextView Implementation of a TextView and all its direct/indirect subclasses with native support for the Roboto fonts, includes the bran

Evgeny Shishkin 782 Nov 12, 2022
A TextView that simulates the effect from the app Secret where the characters fade in/out at different speeds.

SecretTextView A TextView that simulates the effect from the app Secret where the characters fade in/out at different speeds. How To Use Use it just l

Matt Kula 611 Nov 7, 2022
This is based on an open source autosizing textview for Android.

SizeAdjustingTextView This is based on an open source autosizing textview for Android I found a few weeks ago. The initial approach didn't resize mult

Elliott Chenger 255 Dec 29, 2022
An extension of Android's TextView, EditText and Button that let's you use the font of your choice

AnyTextView (deprecated) Note: AnyTextView is no longer being maintained. I recommend replacing AnyTextView with the Calligraphy library instead. Frus

Hans Petter Eide 165 Nov 11, 2022