Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for styling collapsed/expanded text #29

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ADD: Support for styling collapsed/expanded text
  • Loading branch information
milan.r committed Nov 22, 2017
commit 733413c988acd844d38fc110f2cf217be8f185e3
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ protected void onCreate(Bundle savedInstanceState) {
TextView text3 = findViewById(R.id.text3);
text3.setText(getString(R.string.lorem_ipsum3));
TextView text4 = findViewById(R.id.text4);
text4.setText(getString(R.string.one_line_text));
text4.setText(getString(R.string.lorem_ipsum));
TextView text5 = findViewById(R.id.text5);
text5.setText(getString(R.string.one_line_text));
}
}
17 changes: 12 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -13,28 +13,35 @@
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:trimCollapsedText="@string/show_all_content"/>
app:trimCollapsedText="@string/show_all_content" />

<com.borjabravo.readmoretextview.ReadMoreTextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
app:colorClickableText="@color/colorPrimary"/>
app:colorClickableText="@color/colorPrimary" />

<com.borjabravo.readmoretextview.ReadMoreTextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin"
app:showTrimExpandedText="false"
app:trimLength="100"
/>
app:trimLength="100" />

<com.borjabravo.readmoretextview.ReadMoreTextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
app:collapsedTextAppearance="@style/AppTheme.TextAppearance.Collapsed"
app:expandedTextAppearance="@style/AppTheme.TextAppearance.Expanded"
app:trimCollapsedText="@string/show_all_content" />

<com.borjabravo.readmoretextview.ReadMoreTextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin" />

</LinearLayout>
13 changes: 13 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -17,4 +17,17 @@

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="AppTheme.TextAppearance.Collapsed" parent="android:TextAppearance">
<item name="android:textColor">@android:color/holo_blue_dark</item>
<item name="android:textColorLink">@android:color/holo_blue_dark</item>
<item name="android:textSize">12sp</item>
<item name="android:textStyle">bold</item>
</style>

<style name="AppTheme.TextAppearance.Expanded" parent="android:TextAppearance">
<item name="android:textColor">@android:color/holo_green_dark</item>
<item name="android:textColorLink">@android:color/holo_green_dark</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">italic</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -19,12 +19,14 @@
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.StyleRes;
import android.support.v4.content.ContextCompat;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.TextAppearanceSpan;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
@@ -40,6 +42,11 @@ public class ReadMoreTextView extends TextView {
private static final boolean DEFAULT_SHOW_TRIM_EXPANDED_TEXT = true;
private static final String ELLIPSIZE = "... ";

@StyleRes
private final int expandedTextAppearance;
@StyleRes
private final int collapsedTextAppearance;

private CharSequence text;
private BufferType bufferType;
private boolean readMore = true;
@@ -74,6 +81,8 @@ public ReadMoreTextView(Context context, AttributeSet attrs) {
this.showTrimExpandedText =
typedArray.getBoolean(R.styleable.ReadMoreTextView_showTrimExpandedText, DEFAULT_SHOW_TRIM_EXPANDED_TEXT);
this.trimMode = typedArray.getInt(R.styleable.ReadMoreTextView_trimMode, TRIM_MODE_LINES);
this.expandedTextAppearance = typedArray.getResourceId(R.styleable.ReadMoreTextView_expandedTextAppearance, -1);
this.collapsedTextAppearance = typedArray.getResourceId(R.styleable.ReadMoreTextView_collapsedTextAppearance, -1);
typedArray.recycle();
viewMoreSpan = new ReadMoreClickableSpan();
onGlobalLayoutLineEndIndex();
@@ -137,12 +146,14 @@ private CharSequence updateCollapsedText() {
SpannableStringBuilder s = new SpannableStringBuilder(text, 0, trimEndIndex)
.append(ELLIPSIZE)
.append(trimCollapsedText);
addTextStyleSpan(collapsedTextAppearance, s, trimCollapsedText);
return addClickableSpan(s, trimCollapsedText);
}

private CharSequence updateExpandedText() {
if (showTrimExpandedText) {
SpannableStringBuilder s = new SpannableStringBuilder(text, 0, text.length()).append(trimExpandedText);
addTextStyleSpan(expandedTextAppearance, s, trimExpandedText);
return addClickableSpan(s, trimExpandedText);
}
return text;
@@ -153,6 +164,12 @@ private CharSequence addClickableSpan(SpannableStringBuilder s, CharSequence tri
return s;
}

private CharSequence addTextStyleSpan( int textAppearance, SpannableStringBuilder s, CharSequence text) {
s.setSpan(new TextAppearanceSpan(getContext(), textAppearance), s.length() - text.length(),
s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return s;
}

public void setTrimLength(int trimLength) {
this.trimLength = trimLength;
setText();
6 changes: 4 additions & 2 deletions readmoretextview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ReadMoreTextView">
<attr name="expandedTextAppearance" format="reference" />
<attr name="collapsedTextAppearance" format="reference" />
<attr name="trimExpandedText" format="string" />
<attr name="trimCollapsedText" format="string" />
<attr name="trimLength" format="integer" />
<attr name="showTrimExpandedText" format="boolean" />
<attr name="colorClickableText" format="color" />
<attr name="trimLines" format="integer" />
<attr name="trimMode">
<enum name="trimModeLine" value="0"/>
<enum name="trimModeLength" value="1"/>
<enum name="trimModeLine" value="0" />
<enum name="trimModeLength" value="1" />
</attr>
</declare-styleable>
</resources>