Skip to content

Commit c338f83

Browse files
authored
Merge pull request #7 from zsolt-szecsi/rich-text-support
Add rich text support to handle html tags contained by the text parameter
2 parents 59c2061 + 1525a98 commit c338f83

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

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

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

1818
import android.content.Context;
19-
import android.graphics.Color;
2019
import android.support.v7.widget.RecyclerView;
20+
import android.text.Html;
2121
import android.view.LayoutInflater;
2222
import android.view.View;
2323
import android.view.ViewGroup;
@@ -62,8 +62,11 @@ public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
6262

6363
@Override
6464
public void onBindViewHolder(ViewHolder holder, int position) {
65-
66-
readMoreOption.addReadMoreTo(holder.mTextView,context.getString(R.string.dummy_text));
65+
if (position % 2 == 0) {
66+
readMoreOption.addReadMoreTo(holder.mTextView, Html.fromHtml(context.getString(R.string.dummy_text)));
67+
} else {
68+
readMoreOption.addReadMoreTo(holder.mTextView, Html.fromHtml(context.getString(R.string.dummy_text)).toString());
69+
}
6770
}
6871

6972
// Return the size of your dataset (invoked by the layout manager)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
<resources>
1717
<string name="app_name">ReadMoreOption</string>
18-
<string name="dummy_text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</string>
18+
<string name="dummy_text"> <![CDATA[ <b>Lorem</b> <i>Ipsum</i> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.]]></string>
1919
</resources>

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import android.graphics.Color;
2121
import android.os.Build;
2222
import android.os.Handler;
23+
import android.text.Spannable;
2324
import android.text.SpannableString;
25+
import android.text.SpannableStringBuilder;
2426
import android.text.Spanned;
2527
import android.text.TextPaint;
2628
import android.text.method.LinkMovementMethod;
@@ -63,8 +65,7 @@ private ReadMoreOption(Builder builder){
6365
this.expandAnimation = builder.expandAnimation;
6466
}
6567

66-
public void addReadMoreTo(final TextView textView, final String text){
67-
68+
public void addReadMoreTo(final TextView textView, final CharSequence text){
6869
if(textLengthType==TYPE_CHARACTER) {
6970
if (text.length() <= textLength) {
7071
textView.setText(text);
@@ -93,12 +94,16 @@ public void run() {
9394

9495
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) textView.getLayoutParams();
9596

96-
String subString = text.substring(textView.getLayout().getLineStart(0),
97+
String subString = text.toString().substring(textView.getLayout().getLineStart(0),
9798
textView.getLayout().getLineEnd(textLength - 1));
9899
textLengthNew = subString.length() - (moreLabel.length()+4+(lp.rightMargin/6));
99100
}
100101

101-
SpannableString ss = new SpannableString(text.substring(0, textLengthNew) + "... "+ moreLabel);
102+
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text.subSequence(0, textLengthNew))
103+
.append("...")
104+
.append(moreLabel);
105+
106+
SpannableString ss = SpannableString.valueOf(spannableStringBuilder);
102107
ClickableSpan clickableSpan = new ClickableSpan() {
103108
@Override
104109
public void onClick(View view) {
@@ -123,13 +128,16 @@ public void updateDrawState(TextPaint ds) {
123128
textView.setMovementMethod(LinkMovementMethod.getInstance());
124129
}
125130
});
126-
127-
128131
}
129132

130-
private void addReadLess(final TextView textView, final String text ) {
133+
private void addReadLess(final TextView textView, final CharSequence text) {
131134
textView.setMaxLines(Integer.MAX_VALUE);
132-
SpannableString ss = new SpannableString(text + " "+ lessLabel);
135+
136+
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text)
137+
.append(lessLabel);
138+
139+
SpannableString ss = SpannableString.valueOf(spannableStringBuilder);
140+
133141
ClickableSpan clickableSpan = new ClickableSpan() {
134142
@Override
135143
public void onClick(View view) {

0 commit comments

Comments
 (0)