Skip to content

Commit e2ed980

Browse files
committed
fix bug on text contains line separator
1 parent 78da2c5 commit e2ed980

File tree

12 files changed

+57
-53
lines changed

12 files changed

+57
-53
lines changed

app/build.gradle

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ plugins {
33
}
44

55
android {
6-
compileSdk 31
6+
compileSdk 33
77

88
defaultConfig {
99
applicationId "com.erif.readmoretextview"
1010
minSdk 21
11-
targetSdk 31
11+
targetSdk 33
1212
versionCode 1
1313
versionName "1.0"
1414

@@ -25,11 +25,12 @@ android {
2525
sourceCompatibility JavaVersion.VERSION_1_8
2626
targetCompatibility JavaVersion.VERSION_1_8
2727
}
28+
namespace 'com.erif.readmoretextview'
2829
}
2930

3031
dependencies {
3132

32-
implementation 'androidx.appcompat:appcompat:1.4.2'
33+
implementation 'androidx.appcompat:appcompat:1.5.1'
3334
implementation 'com.google.android.material:material:1.6.1'
3435
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
3536
implementation project(':readMoreTextView')

app/src/main/AndroidManifest.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.erif.readmoretextview">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43

54
<application
65
android:allowBackup="true"

app/src/main/java/com/erif/readmoretextview/MainActivity.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ protected void onCreate(Bundle savedInstanceState) {
1717
TextViewReadMore txtReadMore = findViewById(R.id.txtReadMore);
1818
txtReadMore.onClickExpand(v -> txtReadMore.toggle());
1919
txtReadMore.onClickCollapse(v -> txtReadMore.toggle());
20-
txtReadMore.toggleListener(collapsed -> {
21-
22-
});
20+
txtReadMore.toggleListener(collapsed -> debug("Collapsed: "+collapsed));
2321

2422
findViewById(R.id.btnSet).setOnClickListener(v -> {
2523
String lorem = getResources().getString(R.string.lorem_ipsum);

app/src/main/java/com/erif/readmoretextview/MainActivityRecyclerView.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
3939
getString(R.string.lorem_ngawur_kabeh2),
4040
getString(R.string.lorem_ipsum1),
4141
getString(R.string.lorem_ipsum2),
42-
getString(R.string.lorem_ipsum3),
42+
"a",
4343
getString(R.string.lorem_ipsum1),
4444
getString(R.string.lorem_ipsum2),
4545
getString(R.string.lorem_ipsum3),

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@
1111
android:id="@+id/txtReadMore"
1212
android:layout_width="wrap_content"
1313
android:layout_height="wrap_content"
14+
android:layout_marginStart="10dp"
15+
android:layout_marginTop="10dp"
16+
android:layout_marginEnd="10dp"
1417
android:background="#e3e3e3"
15-
android:padding="10dp"
1618
app:readMoreMaxLines="3"
1719
app:expandText="@string/read_more"
1820
app:expandTextStyle="bold|italic"
1921
app:expandTextColor="@color/teal_700"
2022
app:expandTextUnderline="true"
21-
22-
app:collapseText="@string/read_less"
23-
app:collapseTextStyle="italic"
23+
app:collapseText="Closing"
24+
app:collapseTextStyle="bold"
2425
app:collapseTextColor="@color/teal_700"
25-
app:collapseTextUnderline="true"
2626
app:actionClickColor="@color/teal_200"
27-
android:text="@string/lorem_ngawur2"/>
27+
android:text="@string/lorem_ngawur_kabeh"
28+
app:ellipsisType="dots"/>
2829

2930
<Button
3031
android:id="@+id/btnSet"

app/src/main/res/values/strings.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@
7474

7575
<string name="lorem_ngawur_wur">2Lorem ipsum dolor sit amet, consectetur adipiscing elit.
7676
Maecenas sodales rutrum est sed efficitur. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
77-
Fusce eget ligula sed dolor accumsan tristique id vel turpis. Nam congue nunc hendrerit eros fermentum eleifend.
78-
Sed tristique facilisis imperdiet. Donec eget interdum dui. Sed tristique facilisis imperdiet. Donec</string>
77+
Fusce eget ligula sed dolor accumsan tristique id vel turpis.\nNam congue nunc hendrerit eros fermentum eleifend.
78+
Sed tristique facilisis imperdiet. Donec eget interdum dui. Sed tristique facilisis imperdiet. Donec cavd asdf</string>
7979

8080
<string name="lorem_ngawur_kabeh">1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111S11111111111111111111111111111111111111111111111111111111111111E</string>
8181
<string name="lorem_ngawur_kabeh2">abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXBukaDong</string>
8282

83-
<string name="read_more">Buka Dong</string>
84-
<string name="read_less">Tutup Ah</string>
83+
<string name="read_more">Read More</string>
84+
<string name="read_less">Read Less</string>
8585

8686
</resources>

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '7.2.2' apply false
4-
id 'com.android.library' version '7.2.2' apply false
3+
id 'com.android.application' version '7.3.0' apply false
4+
id 'com.android.library' version '7.3.0' apply false
55
}
66

77
task clean(type: Delete) {
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Wed Apr 27 11:08:19 WIB 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

readMoreTextView/build.gradle

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ plugins {
33
}
44

55
android {
6-
compileSdk 31
6+
compileSdk 33
77

88
defaultConfig {
99
minSdk 21
10-
targetSdk 31
10+
targetSdk 33
1111

1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
consumerProguardFiles "consumer-rules.pro"
@@ -23,11 +23,12 @@ android {
2323
sourceCompatibility JavaVersion.VERSION_1_8
2424
targetCompatibility JavaVersion.VERSION_1_8
2525
}
26+
namespace 'com.erif.readmoretextview'
2627
}
2728

2829
dependencies {
2930

30-
implementation 'androidx.appcompat:appcompat:1.4.2'
31+
implementation 'androidx.appcompat:appcompat:1.5.1'
3132
implementation 'com.google.android.material:material:1.6.1'
3233
testImplementation 'junit:junit:4.13.2'
3334
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.erif.readmoretextview">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
43

54
</manifest>

readMoreTextView/src/main/java/com/erif/readmoretextview/TextViewReadMore.java

+26-26
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import android.text.style.StyleSpan;
2525
import android.text.style.UnderlineSpan;
2626
import android.util.AttributeSet;
27-
import android.util.Log;
2827
import android.view.View;
2928
import android.view.ViewGroup;
3029

@@ -137,6 +136,10 @@ public void collapsed(boolean collapsed) {
137136
rebuild = true;
138137
}
139138

139+
public boolean isCollapsed() {
140+
return this.collapsed;
141+
}
142+
140143
@Override
141144
public void setText(CharSequence text, BufferType type) {
142145
if (isAnimate) {
@@ -199,14 +202,14 @@ private boolean isEllipsized(String text) {
199202

200203
private void collapsedBuilder() {
201204
StaticLayout layout;
202-
String replaceSpace = textReplaceSpace();
205+
//String replaceSpace = textReplaceSpace();
203206
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
204207
layout = StaticLayout.Builder
205-
.obtain(replaceSpace, 0, replaceSpace.length(), getPaint(), lineWidth)
208+
.obtain(text, 0, text.length(), getPaint(), lineWidth)
206209
.setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier())
207210
.build();
208211
} else {
209-
layout = new StaticLayout(replaceSpace, getPaint(), lineWidth, Layout.Alignment.ALIGN_NORMAL,
212+
layout = new StaticLayout(text, getPaint(), lineWidth, Layout.Alignment.ALIGN_NORMAL,
210213
getLineSpacingMultiplier(), getLineSpacingExtra(), true
211214
);
212215
}
@@ -217,7 +220,7 @@ private void collapsedBuilder() {
217220
int count = (int) layout.getLineWidth(i);
218221
int start = layout.getLineStart(i);
219222
int end = layout.getLineEnd(i);
220-
lastLineLetter = replaceSpace.subSequence(start, end);
223+
lastLineLetter = text.subSequence(start, end);
221224
lastLineLetterCount = lastLineLetter.length();
222225
sumLineWidth+=count;
223226
}
@@ -236,7 +239,7 @@ private void collapsedBuilder() {
236239
if (sumLineWidth < doubleExpandWith) {
237240
truncatedTextWidth = sumLineWidth;
238241
}
239-
CharSequence truncatedText = TextUtils.ellipsize(replaceSpace, getPaint(), truncatedTextWidth, TextUtils.TruncateAt.END);
242+
CharSequence truncatedText = TextUtils.ellipsize(text, getPaint(), truncatedTextWidth, TextUtils.TruncateAt.END);
240243
String exp = expandText.replaceAll(" ", SPACE_CODE);
241244
String finalText = truncatedText.toString();
242245
if (ellipsisType == ELLIPSIS_TYPE_NONE) {
@@ -292,9 +295,9 @@ public void updateDrawState(@NonNull TextPaint ds) {
292295
}
293296

294297
private void expandBuilder() {
295-
String replaceSpace = textReplaceSpace();
298+
//String replaceSpace = textReplaceSpace();
296299
String collapsedTextSpace = collapseText.replaceAll(" ", SPACE_CODE);
297-
String fullText = replaceSpace+SPACE_CODE+collapsedTextSpace;
300+
String fullText = text+SPACE_CODE+collapsedTextSpace;
298301
spanExpanded = spanExpanded(fullText);
299302
}
300303

@@ -352,50 +355,51 @@ public void toggle() {
352355

353356
if (collapsed) {
354357
String collapsedTextSpace = collapseText.replaceAll(" ", SPACE_CODE);
355-
String fullText = textReplaceSpace()+SPACE_CODE+collapsedTextSpace;
358+
String fullText = text+SPACE_CODE+collapsedTextSpace;
359+
356360
StaticLayout staticFull;
357361
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
358362
staticFull = StaticLayout.Builder
359363
.obtain(fullText, 0, fullText.length(), getPaint(), lineWidth)
360364
.setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier())
365+
.setIncludePad(getIncludeFontPadding())
361366
.build();
362367
} else {
363368
staticFull = new StaticLayout(
364369
fullText, getPaint(), lineWidth, Layout.Alignment.ALIGN_NORMAL,
365-
getLineSpacingMultiplier(), getLineSpacingExtra(), true
370+
getLineSpacingMultiplier(), getLineSpacingExtra(), getIncludeFontPadding()
366371
);
367372
}
368-
fullHeight = staticFull.getHeight() + getCompoundPaddingTop() + getCompoundPaddingBottom();
373+
fullHeight = staticFull.getHeight() + getPaddingTop() + getPaddingBottom();
369374
} else {
370375
StaticLayout staticHalf;
371376
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
372377
staticHalf = StaticLayout.Builder
373-
.obtain(textReplaceSpace(), 0, textReplaceSpace().length(), getPaint(), lineWidth)
378+
.obtain(text, 0, text.length(), getPaint(), lineWidth)
374379
.setMaxLines(maxLines)
375380
.setEllipsize(TextUtils.TruncateAt.END)
376381
.setLineSpacing(getLineSpacingExtra(), getLineSpacingMultiplier())
382+
.setIncludePad(getIncludeFontPadding())
377383
.build();
378384
} else {
379385
int maxLength = text.length();
380386
do {
381387
staticHalf = new StaticLayout(
382388
ellipsize(text, maxLength), getPaint(), lineWidth, Layout.Alignment.ALIGN_NORMAL,
383-
getLineSpacingMultiplier(), getLineSpacingExtra(), true
389+
getLineSpacingMultiplier(), getLineSpacingExtra(), getIncludeFontPadding()
384390
);
385391
maxLength -= 10;
386392
} while (staticHalf.getLineCount() > 2);
387393
}
388-
halfHeight = staticHalf.getHeight() + getCompoundPaddingTop() + getCompoundPaddingBottom();
394+
halfHeight = staticHalf.getHeight() + getPaddingTop() + getPaddingTop();
389395
}
390-
391-
//int start = collapsed ? halfHeight : fullHeight;
392-
int start = getHeight();
393396
int end = collapsed ? fullHeight : halfHeight;
394-
ValueAnimator anim = ValueAnimator.ofInt(start, end);
397+
ValueAnimator anim = ValueAnimator.ofInt(getHeight(), end);
395398
anim.setDuration(animationDuration);
396399
ViewGroup.LayoutParams params = getLayoutParams();
397400
anim.addUpdateListener(animation -> {
398-
params.height = (int) animation.getAnimatedValue();
401+
Object value = animation.getAnimatedValue();
402+
params.height = (int) value;
399403
setLayoutParams(params);
400404
});
401405
anim.addListener(new AnimatorListenerAdapter() {
@@ -404,8 +408,7 @@ public void onAnimationStart(Animator animation) {
404408
super.onAnimationStart(animation);
405409
isAnimate = true;
406410
if (collapsed) {
407-
String replaceSpace = textReplaceSpace();
408-
setText(replaceSpace, BufferType.SPANNABLE);
411+
setText(text, BufferType.SPANNABLE);
409412
}
410413
}
411414
@Override
@@ -461,17 +464,14 @@ private String ellipsize(String text, int size) {
461464
}
462465
}
463466

467+
/* Unused function
464468
private void debug(String message) {
465469
Log.d("TextViewReadMore", message);
466470
}
467-
468-
public interface ToggleListener {
469-
public void onToggle(boolean collapsed);
470-
}
471-
472471
private String textReplaceSpace() {
473472
return text.replaceAll(" ", SPACE_CODE);
474473
}
474+
*/
475475

476476
private void setWrapLayout() {
477477
ViewGroup.LayoutParams params = getLayoutParams();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.erif.readmoretextview;
2+
3+
public interface ToggleListener {
4+
public void onToggle(boolean collapsed);
5+
}

0 commit comments

Comments
 (0)