1515
1616package com .devs .readmoreoption ;
1717
18+ import android .animation .LayoutTransition ;
1819import android .content .Context ;
1920import android .graphics .Color ;
21+ import android .os .Build ;
2022import android .os .Handler ;
2123import android .text .SpannableString ;
2224import android .text .Spanned ;
2325import android .text .TextPaint ;
2426import android .text .method .LinkMovementMethod ;
2527import android .text .style .ClickableSpan ;
28+ import android .util .Log ;
2629import android .view .View ;
30+ import android .view .ViewGroup ;
2731import android .widget .TextView ;
2832
2933/**
3034 * Created by ${Deven} on 6/1/18.
3135 */
3236public class ReadMoreOption {
3337
38+ private static final String TAG = ReadMoreOption .class .getSimpleName ();
39+ public static final int TYPE_LINE = 1 ;
40+ public static final int TYPE_CHARACTER = 2 ;
41+
3442 // required
3543 private Context context ;
3644 // optional
3745 private int textLength ;
46+ private int textLengthType ;
3847 private String moreLabel ;
3948 private String lessLabel ;
4049 private int moreLabelColor ;
4150 private int lessLabelColor ;
4251 private boolean labelUnderLine ;
52+ private boolean expandAnimation ;
4353
4454 private ReadMoreOption (Builder builder ){
4555 this .context = builder .context ;
4656 this .textLength = builder .textLength ;
57+ this .textLengthType = builder .textLengthType ;
4758 this .moreLabel = builder .moreLabel ;
4859 this .lessLabel = builder .lessLabel ;
4960 this .moreLabelColor = builder .moreLabelColor ;
5061 this .lessLabelColor = builder .lessLabelColor ;
5162 this .labelUnderLine = builder .labelUnderLine ;
63+ this .expandAnimation = builder .expandAnimation ;
5264 }
5365
5466 public void addReadMoreTo (final TextView textView , final String text ){
5567
56- if (text .length () <= textLength ) {
68+ if (textLengthType ==TYPE_CHARACTER ) {
69+ if (text .length () <= textLength ) {
70+ textView .setText (text );
71+ return ;
72+ }
73+ }
74+ else {
75+ // If TYPE_LINE
76+ textView .setLines (textLength );
5777 textView .setText (text );
58- return ;
5978 }
6079
61- SpannableString ss = new SpannableString (text .substring (0 , textLength ) + "... " + moreLabel );
62- ClickableSpan clickableSpan = new ClickableSpan () {
80+ textView .post (new Runnable () {
6381 @ Override
64- public void onClick (View view ) {
65- addReadLess (textView , text );
66- }
67- @ Override
68- public void updateDrawState (TextPaint ds ) {
69- super .updateDrawState (ds );
70- ds .setUnderlineText (labelUnderLine );
71- ds .setColor (moreLabelColor );
82+ public void run () {
83+
84+ int textLengthNew = textLength ;
85+
86+ if (textLengthType ==TYPE_LINE ) {
87+
88+
89+ if (textView .getLayout ().getLineCount () <= textLength ) {
90+ textView .setText (text );
91+ return ;
92+ }
93+
94+ ViewGroup .MarginLayoutParams lp = (ViewGroup .MarginLayoutParams ) textView .getLayoutParams ();
95+
96+ String subString = text .substring (textView .getLayout ().getLineStart (0 ),
97+ textView .getLayout ().getLineEnd (textLength - 1 ));
98+ textLengthNew = subString .length () - (moreLabel .length ()+4 +(lp .rightMargin /6 ));
99+ }
100+
101+ SpannableString ss = new SpannableString (text .substring (0 , textLengthNew ) + "... " + moreLabel );
102+ ClickableSpan clickableSpan = new ClickableSpan () {
103+ @ Override
104+ public void onClick (View view ) {
105+ addReadLess (textView , text );
106+ }
107+ @ Override
108+ public void updateDrawState (TextPaint ds ) {
109+ super .updateDrawState (ds );
110+ ds .setUnderlineText (labelUnderLine );
111+ ds .setColor (moreLabelColor );
112+ }
113+ };
114+ ss .setSpan (clickableSpan , ss .length () - moreLabel .length (), ss .length () , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
115+
116+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN && expandAnimation ) {
117+ LayoutTransition layoutTransition = new LayoutTransition ();
118+ layoutTransition .enableTransitionType (LayoutTransition .CHANGING );
119+ ((ViewGroup )textView .getParent ()).setLayoutTransition (layoutTransition );
120+ }
121+
122+ textView .setText (ss );
123+ textView .setMovementMethod (LinkMovementMethod .getInstance ());
72124 }
73- };
74- ss .setSpan (clickableSpan , ss .length () - moreLabel .length (), ss .length () , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
75- textView .setText (ss )
76- ;
77- textView .setMovementMethod (LinkMovementMethod .getInstance ());
125+ });
126+
127+
78128 }
79129
80130 private void addReadLess (final TextView textView , final String text ) {
131+ textView .setMaxLines (Integer .MAX_VALUE );
81132 SpannableString ss = new SpannableString (text + " " + lessLabel );
82133 ClickableSpan clickableSpan = new ClickableSpan () {
83134 @ Override
@@ -97,8 +148,7 @@ public void updateDrawState(TextPaint ds) {
97148 }
98149 };
99150 ss .setSpan (clickableSpan , ss .length () - lessLabel .length (), ss .length () , Spanned .SPAN_EXCLUSIVE_EXCLUSIVE );
100- textView .setText (ss )
101- ;
151+ textView .setText (ss );
102152 textView .setMovementMethod (LinkMovementMethod .getInstance ());
103153 }
104154
@@ -107,18 +157,28 @@ public static class Builder {
107157 private Context context ;
108158 // optional
109159 private int textLength = 100 ;
160+ private int textLengthType = ReadMoreOption .TYPE_CHARACTER ;
110161 private String moreLabel = "read more" ;
111162 private String lessLabel = "read less" ;
112163 private int moreLabelColor = Color .parseColor ("#ff00ff" );
113164 private int lessLabelColor = Color .parseColor ("#ff00ff" );
114165 private boolean labelUnderLine = false ;
166+ private boolean expandAnimation = false ;
115167
116168 public Builder (Context context ){
117169 this .context = context ;
118170 }
119171
120- public Builder textLength (int length ){
172+ /**
173+ * @param length can be no. of line OR no. of characters - default is 100 character
174+ * @param textLengthType ReadMoreOption.TYPE_LINE for no. of line OR
175+ * ReadMoreOption.TYPE_CHARACTER for no. of character
176+ * - default is ReadMoreOption.TYPE_CHARACTER
177+ * @return Builder obj
178+ */
179+ public Builder textLength (int length , int textLengthType ){
121180 this .textLength = length ;
181+ this .textLengthType = textLengthType ;
122182 return this ;
123183 }
124184
@@ -147,6 +207,16 @@ public Builder labelUnderLine(boolean labelUnderLine){
147207 return this ;
148208 }
149209
210+ /**
211+ * @param expandAnimation either true to enable animation on expand or false to disable animation
212+ * - default is false
213+ * @return Builder obj
214+ */
215+ public Builder expandAnimation (boolean expandAnimation ){
216+ this .expandAnimation = expandAnimation ;
217+ return this ;
218+ }
219+
150220 public ReadMoreOption build (){
151221 return new ReadMoreOption (this );
152222 }
0 commit comments