-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
23 changes: 22 additions & 1 deletion
23
app/src/main/java/com/iamageo/another_read_more/MainActivity.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,32 @@ | ||
package com.iamageo.another_read_more | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.iamageo.library.AnotherReadMore | ||
|
||
|
||
class MainActivity : AppCompatActivity() { | ||
|
||
private var anotherReadMore: AnotherReadMore? = null | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_main) | ||
|
||
val mtv = findViewById<TextView>(R.id.tv) | ||
|
||
anotherReadMore = AnotherReadMore.Builder(this).build() | ||
|
||
val readMoreOption: AnotherReadMore = AnotherReadMore.Builder(this) | ||
.textLength(100, AnotherReadMore.TYPE_LINE) | ||
.moreLabel("mais") | ||
.lessLabel("menos") | ||
.build() | ||
|
||
readMoreOption.addReadMoreTo( | ||
mtv, | ||
"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." | ||
) | ||
} | ||
} |
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
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
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
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
129 changes: 129 additions & 0 deletions
129
library/src/main/java/com/iamageo/library/AnotherReadMore.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.iamageo.library | ||
|
||
import android.animation.LayoutTransition | ||
import android.content.Context | ||
import android.os.Handler | ||
import android.text.SpannableString | ||
import android.text.SpannableStringBuilder | ||
import android.text.Spanned | ||
import android.text.method.LinkMovementMethod | ||
import android.text.style.ClickableSpan | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.view.ViewGroup.MarginLayoutParams | ||
import android.widget.TextView | ||
|
||
class AnotherReadMore( | ||
var context: Context? = null, | ||
val textLength: Int = 0, | ||
val textLengthType: Int = 0, | ||
val moreLabel: String? = null, | ||
val lessLabel: String? = null, | ||
) { | ||
|
||
private constructor(builder: Builder) : this( | ||
builder.context, | ||
builder.textLength, | ||
builder.textLengthType, | ||
builder.moreLabel, | ||
builder.lessLabel, | ||
) | ||
|
||
fun addReadMoreTo(textView: TextView, text: CharSequence) { | ||
if (textLengthType == AnotherReadMore.TYPE_CHARACTER) { | ||
if (text.length <= textLength) { | ||
textView.text = text | ||
return | ||
} | ||
} else { | ||
// If TYPE_LINE | ||
textView.setLines(textLength) | ||
textView.text = text | ||
} | ||
textView.post(Runnable { | ||
var textLengthNew = textLength | ||
if (textLengthType == AnotherReadMore.TYPE_LINE) { | ||
if (textView.layout.lineCount <= textLength) { | ||
textView.text = text | ||
return@Runnable | ||
} | ||
val lp = textView.layoutParams as MarginLayoutParams | ||
val subString = text.toString().substring( | ||
textView.layout.getLineStart(0), | ||
textView.layout.getLineEnd(textLength - 1) | ||
) | ||
textLengthNew = subString.length - (moreLabel!!.length + 4 + lp.rightMargin / 6) | ||
} | ||
val spannableStringBuilder = SpannableStringBuilder(text.subSequence(0, textLengthNew)) | ||
.append("...") | ||
.append(moreLabel) | ||
val ss = SpannableString.valueOf(spannableStringBuilder) | ||
val clickableSpan: ClickableSpan = object : ClickableSpan() { | ||
override fun onClick(view: View) { | ||
addReadLess(textView, text) | ||
} | ||
|
||
} | ||
ss.setSpan( | ||
clickableSpan, | ||
ss.length - moreLabel!!.length, | ||
ss.length, | ||
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | ||
) | ||
val layoutTransition = LayoutTransition() | ||
layoutTransition.enableTransitionType(LayoutTransition.CHANGING) | ||
(textView.parent as ViewGroup).layoutTransition = layoutTransition | ||
textView.text = ss | ||
textView.movementMethod = LinkMovementMethod.getInstance() | ||
}) | ||
} | ||
|
||
private fun addReadLess(textView: TextView, text: CharSequence) { | ||
textView.maxLines = Int.MAX_VALUE | ||
val spannableStringBuilder = SpannableStringBuilder(text) | ||
.append(lessLabel) | ||
val ss = SpannableString.valueOf(spannableStringBuilder) | ||
val clickableSpan: ClickableSpan = object : ClickableSpan() { | ||
override fun onClick(view: View) { | ||
Handler().post { addReadMoreTo(textView, text) } | ||
} | ||
|
||
} | ||
ss.setSpan( | ||
clickableSpan, | ||
ss.length - lessLabel!!.length, | ||
ss.length, | ||
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | ||
) | ||
textView.text = ss | ||
textView.movementMethod = LinkMovementMethod.getInstance() | ||
} | ||
|
||
class Builder(c: Context) { | ||
val context: Context = c | ||
|
||
var textLength = 100 | ||
private set | ||
var textLengthType = AnotherReadMore.TYPE_CHARACTER | ||
private set | ||
var moreLabel = "mais" | ||
private set | ||
var lessLabel = "menos" | ||
private set | ||
|
||
|
||
fun textLength(length: Int?, typeLine: Int) = apply { textLength = length ?: 100 } | ||
fun textLengthType(type: Int) = apply { textLengthType = type } | ||
fun moreLabel(more: String) = apply { moreLabel = more } | ||
fun lessLabel(less: String) = apply { lessLabel = less } | ||
|
||
fun build() = AnotherReadMore(this) | ||
} | ||
|
||
companion object { | ||
private val TAG = AnotherReadMore::class.java.simpleName | ||
const val TYPE_LINE = 1 | ||
const val TYPE_CHARACTER = 2 | ||
} | ||
|
||
} |