Last active
December 5, 2018 09:56
-
-
Save zeero0/ce41ec72040853ee0af641af657e7410 to your computer and use it in GitHub Desktop.
Expandable/Collapsible TextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<app.utils.customviews.SeeMoreTextView | |
android:layout_height="wrap_content" | |
android:layout_width="match_parent" | |
app:ellipsizeColor="@color/grey" | |
app:trimLength="120" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<declare-styleable name="SeeMoreTextView"> | |
<attr name="trimLength" format="integer"/> | |
<attr name="ellipsizeColor" format="reference" /> | |
</declare-styleable> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.utils.customviews; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Color; | |
import android.text.SpannableStringBuilder; | |
import android.text.Spanned; | |
import android.text.TextUtils; | |
import android.text.style.ForegroundColorSpan; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.TextView; | |
public class SeeMoreTextView extends TextView { | |
private static final int DEFAULT_COLOR = Color.rgb(155, 155, 155); | |
private static final int DEFAULT_TRIM_LENGTH = 130; | |
private static final String ELLIPSIS = "... see more"; | |
private CharSequence originalText; | |
private CharSequence trimmedText; | |
private BufferType bufferType; | |
private boolean trim = true; | |
private int trimLength; | |
private int seeMoreColor; | |
public SeeMoreTextView(Context context) { | |
this(context, null); | |
} | |
public SeeMoreTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SeeMoreTextView); | |
this.trimLength = typedArray.getInt(R.styleable.SeeMoreTextView_trimLength, DEFAULT_TRIM_LENGTH); | |
this.seeMoreColor = typedArray.getColor(R.styleable.SeeMoreTextView_ellipsizeColor, DEFAULT_COLOR); | |
typedArray.recycle(); | |
setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
trim = !trim; | |
setText(); | |
requestFocusFromTouch(); | |
} | |
}); | |
} | |
private void setText() { | |
super.setText(getDisplayableText(), bufferType); | |
} | |
private CharSequence getDisplayableText() { | |
return trim ? trimmedText : originalText; | |
} | |
@Override | |
public void setText(CharSequence text, BufferType type) { | |
originalText = text; | |
trimmedText = getTrimmedText(text); | |
bufferType = type; | |
setText(); | |
} | |
private CharSequence getTrimmedText(CharSequence text) { | |
if (originalText != null && originalText.length() > trimLength) { | |
final ForegroundColorSpan colorSpan = new ForegroundColorSpan(seeMoreColor); | |
SpannableStringBuilder seeMoreBuilder = new SpannableStringBuilder(ELLIPSIS); | |
seeMoreBuilder.setSpan(colorSpan, 0, ELLIPSIS.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); | |
SpannableStringBuilder trimmedBuilder = new SpannableStringBuilder(originalText, 0, trimLength + 1); | |
return TextUtils.concat(trimmedBuilder, seeMoreBuilder); | |
} else { | |
return originalText; | |
} | |
} | |
public CharSequence getOriginalText() { | |
return originalText; | |
} | |
public void setTrimLength(int trimLength) { | |
this.trimLength = trimLength; | |
trimmedText = getTrimmedText(originalText); | |
setText(); | |
} | |
public int getTrimLength() { | |
return trimLength; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment