Skip to content

Commit

Permalink
Merge pull request #7 from zsolt-szecsi/rich-text-support
Browse files Browse the repository at this point in the history
Add rich text support to handle html tags contained by the text parameter
  • Loading branch information
devendroid authored Oct 15, 2018
2 parents 59c2061 + 1525a98 commit c338f83
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
9 changes: 6 additions & 3 deletions app/src/main/java/com/devs/readmoreoptiondemo/MyAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package com.devs.readmoreoptiondemo;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -62,8 +62,11 @@ public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

readMoreOption.addReadMoreTo(holder.mTextView,context.getString(R.string.dummy_text));
if (position % 2 == 0) {
readMoreOption.addReadMoreTo(holder.mTextView, Html.fromHtml(context.getString(R.string.dummy_text)));
} else {
readMoreOption.addReadMoreTo(holder.mTextView, Html.fromHtml(context.getString(R.string.dummy_text)).toString());
}
}

// Return the size of your dataset (invoked by the layout manager)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

<resources>
<string name="app_name">ReadMoreOption</string>
<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>
<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>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
Expand Down Expand Up @@ -63,8 +65,7 @@ private ReadMoreOption(Builder builder){
this.expandAnimation = builder.expandAnimation;
}

public void addReadMoreTo(final TextView textView, final String text){

public void addReadMoreTo(final TextView textView, final CharSequence text){
if(textLengthType==TYPE_CHARACTER) {
if (text.length() <= textLength) {
textView.setText(text);
Expand Down Expand Up @@ -93,12 +94,16 @@ public void run() {

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

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

SpannableString ss = new SpannableString(text.substring(0, textLengthNew) + "... "+ moreLabel);
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text.subSequence(0, textLengthNew))
.append("...")
.append(moreLabel);

SpannableString ss = SpannableString.valueOf(spannableStringBuilder);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
Expand All @@ -123,13 +128,16 @@ public void updateDrawState(TextPaint ds) {
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
});


}

private void addReadLess(final TextView textView, final String text ) {
private void addReadLess(final TextView textView, final CharSequence text) {
textView.setMaxLines(Integer.MAX_VALUE);
SpannableString ss = new SpannableString(text + " "+ lessLabel);

SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text)
.append(lessLabel);

SpannableString ss = SpannableString.valueOf(spannableStringBuilder);

ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
Expand Down

0 comments on commit c338f83

Please sign in to comment.