Skip to content

Commit 1302b45

Browse files
committed
updates for the version 1.0.1
1 parent fb670e1 commit 1302b45

9 files changed

Lines changed: 122 additions & 33 deletions

File tree

0 Bytes
Binary file not shown.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,24 @@ Convert your TextView in ExpandableTextView with added options ReadMore/ReadLess
3636
// OR using options to customize
3737

3838
ReadMoreOption readMoreOption = new ReadMoreOption.Builder(this)
39-
.textLength(300)
39+
.textLength(3, ReadMoreOption.TYPE_LINE) // OR
40+
//.textLength(300, ReadMoreOption.TYPE_CHARACTER)
4041
.moreLabel("MORE")
4142
.lessLabel("LESS")
4243
.moreLabelColor(Color.RED)
4344
.lessLabelColor(Color.BLUE)
4445
.labelUnderLine(true)
46+
.expandAnimation(true)
4547
.build();
4648

4749
readMoreOption.addReadMoreTo(textView, getString(R.string.long_desc));
4850

4951
```
5052

53+
### Known Issue
54+
55+
- [ ] expandAnimation not works with ListView/RecyclerView.
56+
5157
## License
5258
```
5359
Copyright 2018 Deven Singh

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
android:roundIcon="@mipmap/ic_launcher_round"
2525
android:supportsRtl="true"
2626
android:theme="@style/AppTheme">
27-
<activity android:name=".MainActivity">
27+
<activity android:name=".MainActivity"
28+
android:screenOrientation="portrait">
2829
<intent-filter>
2930
<action android:name="android.intent.action.MAIN" />
3031

app/src/main/java/com/devs/readmoreoptiondemo/MainActivity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.devs.readmoreoptiondemo;
1717

1818
import android.graphics.Color;
19+
import android.media.TimedText;
1920
import android.support.v7.app.AppCompatActivity;
2021
import android.os.Bundle;
2122
import android.support.v7.widget.DividerItemDecoration;
@@ -44,14 +45,19 @@ protected void onCreate(Bundle savedInstanceState) {
4445
recyclerView.setAdapter(mAdapter);
4546

4647

48+
// TextView tv = (TextView)findViewById(R.id.tv);
49+
// tv.setText(getString(R.string.dummy_text));
50+
//
4751
// ReadMoreOption readMoreOption = new ReadMoreOption.Builder(this)
48-
// // Optional parameters
49-
// .textLength(300)
52+
// // Optional parameters
53+
// .textLength(3, ReadMoreOption.TYPE_LINE) //OR
54+
// //.textLength(300, ReadMoreOption.TYPE_CHARACTER)
5055
// .moreLabel("MORE")
5156
// .lessLabel("LESS")
5257
// .moreLabelColor(Color.RED)
5358
// .lessLabelColor(Color.BLUE)
5459
// .labelUnderLine(true)
60+
// .expandAnimation(true)
5561
// .build();
5662
// readMoreOption.addReadMoreTo(tv, getString(R.string.dummy_text));
5763

app/src/main/java/com/devs/readmoreoptiondemo/MyAdapter.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ static class ViewHolder extends RecyclerView.ViewHolder {
4444
}
4545
}
4646

47-
// Provide a suitable constructor (depends on the kind of dataset)
48-
public MyAdapter(Context context) {
47+
MyAdapter(Context context) {
4948
this.context = context;
5049
readMoreOption = new ReadMoreOption.Builder(context)
5150
.build();
@@ -61,11 +60,8 @@ public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
6160
return new ViewHolder(v);
6261
}
6362

64-
// Replace the contents of a view (invoked by the layout manager)
6563
@Override
6664
public void onBindViewHolder(ViewHolder holder, int position) {
67-
// - get element from your dataset at this position
68-
// - replace the contents of the view with that element
6965
readMoreOption.addReadMoreTo(holder.mTextView,context.getString(R.string.dummy_text));
7066
}
7167

app/src/main/res/layout/activity_main.xml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,22 @@
1313
~ See the License for the specific language governing permissions and
1414
~ limitations under the License.
1515
-->
16-
<LinearLayout
17-
xmlns:android="http://schemas.android.com/apk/res/android"
16+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
1817
android:layout_width="match_parent"
1918
android:layout_height="match_parent"
19+
xmlns:tools="http://schemas.android.com/tools"
2020
android:orientation="vertical">
2121

22+
23+
24+
<!-- <TextView
25+
android:id="@+id/tv"
26+
android:layout_margin="16dp"
27+
tools:text="@string/dummy_text"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content" />-->
30+
31+
2232
<android.support.v7.widget.RecyclerView
2333
android:id="@+id/my_recycler_view"
2434
android:layout_margin="8dp"

readmoreoption/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ android {
2424
minSdkVersion 16
2525
targetSdkVersion 27
2626
versionCode 1
27-
versionName "1.0"
27+
versionName "1.0.1"
2828

2929
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
3030

readmoreoption/src/main/java/com/devs/readmoreoption/ReadMoreOption.java

Lines changed: 90 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,69 +15,120 @@
1515

1616
package com.devs.readmoreoption;
1717

18+
import android.animation.LayoutTransition;
1819
import android.content.Context;
1920
import android.graphics.Color;
21+
import android.os.Build;
2022
import android.os.Handler;
2123
import android.text.SpannableString;
2224
import android.text.Spanned;
2325
import android.text.TextPaint;
2426
import android.text.method.LinkMovementMethod;
2527
import android.text.style.ClickableSpan;
28+
import android.util.Log;
2629
import android.view.View;
30+
import android.view.ViewGroup;
2731
import android.widget.TextView;
2832

2933
/**
3034
* Created by ${Deven} on 6/1/18.
3135
*/
3236
public 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

Comments
 (0)