Java tutorial
// Copyright (c) 2018, Tom Geiselmann // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software // and associated documentation files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, publish, distribute, // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package com.none.tom.simplerssreader.view; import android.content.Context; import android.content.res.TypedArray; import android.support.v4.content.ContextCompat; import android.support.v7.widget.AppCompatTextView; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextPaint; import android.text.TextUtils; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.util.AttributeSet; import android.view.View; import com.none.tom.simplerssreader.R; public class ReadMoreTextView extends AppCompatTextView { public static final int EMS = 100; private CharSequence mText; private final int mColor; public ReadMoreTextView(final Context context) { this(context, null); } public ReadMoreTextView(final Context context, final AttributeSet attrs) { super(context, attrs); final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ReadMoreTextView); mColor = a.getColor(R.styleable.ReadMoreTextView_readMoreColor, ContextCompat.getColor(context, R.color.colorAccent)); a.recycle(); } @SuppressWarnings("SameParameterValue") public void setText(final CharSequence text, final int maxEms) { if (!TextUtils.isEmpty(text) && text.length() > maxEms) { final SpannableStringBuilder ssb = new SpannableStringBuilder(text.subSequence(0, maxEms)); ssb.append('\u2026'); final int textLength = ssb.length(); ssb.append('\n'); ssb.append(getContext().getString(R.string.read_more)); final int readMoreLength = ssb.length() - textLength; ssb.setSpan(new ReadMoreClickableSpan(), textLength, textLength + readMoreLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); super.setText(ssb); setMaxEms(maxEms + readMoreLength); setMovementMethod(LinkMovementMethod.getInstance()); mText = text; } else { super.setText(text); } } private class ReadMoreClickableSpan extends ClickableSpan { @Override public void onClick(final View view) { setText(mText); setMaxEms(Integer.MAX_VALUE); } @Override public void updateDrawState(final TextPaint paint) { paint.setUnderlineText(false); paint.setColor(mColor); } } }