Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ ShowMoreLess.Builder(this)
.showMoreLabelColor(R.color.colorPrimaryDark)
.showLessLabelColor(R.color.colorPrimaryDark)
.labelUnderLine(labelUnderLine = false)
.labelBold(labelBold = false)
.expandAnimation(expandAnimation = true)
.textClickable(
textClickableInExpand = true,
Expand All @@ -57,7 +58,7 @@ ShowMoreLess.Builder(this)
.build().apply {
addShowMoreLess(textView = tv_first, text = tv_first.text, isContentExpanded = true)
setListener(object : ShowMoreLess.OnShowMoreLessClickedListener {

override fun onShowMoreClicked() {
//We can handle or save show more state
}
Expand All @@ -67,10 +68,10 @@ ShowMoreLess.Builder(this)
}
}
)
}
}
```

# Customization
# Customization
## You can customize things like bellow

##### 1. Can change *textLengthAndLengthType* as
Expand Down Expand Up @@ -107,13 +108,19 @@ ShowMoreLess.Builder(this)
.labelUnderLine(labelUnderLine = false)//true for underline and false for not underline
```

##### 5. Can easily handle expand and collapse *animation* by
##### 5. Can enable *labelBold* easily as

```Kotlin
.labelBold(labelBold = false)//true for bold and false for normal
```

##### 6. Can easily handle expand and collapse *animation* by

```Kotlin
.expandAnimation(expandAnimation = true)//expandAnimation will affect both expand and collapse logic
```

##### 6. Can handle *textClickable* except clicking more or less txt for expand and collapse
##### 7. Can handle *textClickable* except clicking more or less txt for expand and collapse

```Kotlin
.textClickable(
Expand All @@ -122,7 +129,7 @@ ShowMoreLess.Builder(this)
)
```

##### 7. *addShowMoreLess()* function will handle text expand or collapse state
##### 8. *addShowMoreLess()* function will handle text expand or collapse state

```Kotlin
addShowMoreLess(
Expand All @@ -132,7 +139,7 @@ addShowMoreLess(
)
```

##### 8. At last *setListener()* function for callbacks
##### 9. At last *setListener()* function for callbacks

```Kotlin
setListener(object : ShowMoreLess.OnShowMoreLessClickedListener {
Expand Down
59 changes: 30 additions & 29 deletions app/src/main/java/com/noowenz/showmoreless/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,36 @@ class MainActivity : AppCompatActivity() {

private fun initViews() {
ShowMoreLess.Builder(this)
/*.textLengthAndLengthType(
length = 100,
textLengthType = ShowMoreLess.TYPE_CHARACTER
)*/
.textLengthAndLengthType(
length = 5,
textLengthType = ShowMoreLess.TYPE_LINE
)
.showMoreLabel("show more")
.showLessLabel("show less")
.showMoreLabelColor(Color.parseColor("#FF0000"))
.showLessLabelColor(Color.parseColor("#FF0000"))
.labelUnderLine(labelUnderLine = true)
.expandAnimation(expandAnimation = true)
.textClickable(
textClickableInExpand = true,
textClickableInCollapse = true
)
.build().apply {
addShowMoreLess(textView = tv_first, text = tv_first.text, isContentExpanded = false)
setListener(object : ShowMoreLess.OnShowMoreLessClickedListener {
override fun onShowMoreClicked() {
//We can handle or save show more state
}
/*.textLengthAndLengthType(
length = 100,
textLengthType = ShowMoreLess.TYPE_CHARACTER
)*/
.textLengthAndLengthType(
length = 5,
textLengthType = ShowMoreLess.TYPE_LINE
)
.showMoreLabel("show more")
.showLessLabel("show less")
.showMoreLabelColor(Color.parseColor("#FF0000"))
.showLessLabelColor(Color.parseColor("#FF0000"))
.labelUnderLine(labelUnderLine = true)
.labelBold(labelBold = true)
.expandAnimation(expandAnimation = true)
.textClickable(
textClickableInExpand = true,
textClickableInCollapse = true
)
.build().apply {
addShowMoreLess(textView = tv_first, text = tv_first.text, isContentExpanded = false)
setListener(object : ShowMoreLess.OnShowMoreLessClickedListener {
override fun onShowMoreClicked() {
//We can handle or save show more state
}

override fun onShowLessClicked() {
//We can handle or save show less state
}
})
}
override fun onShowLessClicked() {
//We can handle or save show less state
}
})
}
}
}
35 changes: 24 additions & 11 deletions showmoreless/src/main/java/com/noowenz/showmoreless/ShowMoreLess.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ShowMoreLess private constructor(builder: Builder) {
private val moreLabelColor: Int
private val lessLabelColor: Int
private val labelUnderLine: Boolean
private val labelBold: Boolean
private val expandAnimation: Boolean
private val textClickableInExpand: Boolean
private val textClickableInCollapse: Boolean
Expand All @@ -54,15 +55,16 @@ class ShowMoreLess private constructor(builder: Builder) {
this.moreLabelColor = builder.moreLabelColor
this.lessLabelColor = builder.lessLabelColor
this.labelUnderLine = builder.labelUnderLine
this.labelBold = builder.labelBold
this.expandAnimation = builder.expandAnimation
this.textClickableInExpand = builder.textClickableInExpand
this.textClickableInCollapse = builder.textClickableInCollapse
}

fun addShowMoreLess(
textView: TextView,
text: CharSequence,
isContentExpanded: Boolean
textView: TextView,
text: CharSequence,
isContentExpanded: Boolean
) {
if (textLengthType == TYPE_CHARACTER) {
if (text.length <= textLength) {
Expand Down Expand Up @@ -114,16 +116,16 @@ class ShowMoreLess private constructor(builder: Builder) {
}

private fun addShowMore(
textView: TextView,
trimText: CharSequence
textView: TextView,
trimText: CharSequence
) {
try {
val newSubString: CharSequence
if (textLengthType == TYPE_LINE) {
val lp = textView.layoutParams as ViewGroup.MarginLayoutParams
val subString = trimText.substring(
startIndex = textView.layout.getLineStart(0),
endIndex = textView.layout.getLineEnd(textLength - 1)
startIndex = textView.layout.getLineStart(0),
endIndex = textView.layout.getLineEnd(textLength - 1)
)
newSubString = if (!subString.endsWith("\n", false)) {
val startRange = subString.length - (moreLabel.length + 4 + lp.rightMargin / 6)
Expand All @@ -140,7 +142,7 @@ class ShowMoreLess private constructor(builder: Builder) {
newSubString = trimText.subSequence(0, textLength)
}
val spannableStringBuilder = SpannableStringBuilder(
newSubString
newSubString
).apply {
this.append("...")
this.append(moreLabel)
Expand All @@ -156,6 +158,7 @@ class ShowMoreLess private constructor(builder: Builder) {
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = labelUnderLine
ds.isFakeBoldText = labelBold
ds.color = moreLabelColor
}
}
Expand Down Expand Up @@ -205,8 +208,8 @@ class ShowMoreLess private constructor(builder: Builder) {
}

private fun addShowLess(
textView: TextView,
trimText: CharSequence
textView: TextView,
trimText: CharSequence
) {
try {
textView.maxLines = Integer.MAX_VALUE
Expand All @@ -229,6 +232,7 @@ class ShowMoreLess private constructor(builder: Builder) {
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = labelUnderLine
ds.isFakeBoldText = labelBold
ds.color = lessLabelColor
}
}
Expand Down Expand Up @@ -313,7 +317,7 @@ class ShowMoreLess private constructor(builder: Builder) {
}

class Builder(// required
val context: Context) {
val context: Context) {
// optional
var textLength = 100
var textLengthType = TYPE_CHARACTER
Expand All @@ -322,6 +326,7 @@ class ShowMoreLess private constructor(builder: Builder) {
var moreLabelColor = Color.parseColor("#ffffff")
var lessLabelColor = Color.parseColor("#ffffff")
var labelUnderLine = false
var labelBold = false
var expandAnimation = false
var textClickableInExpand = false
var textClickableInCollapse = false
Expand Down Expand Up @@ -380,6 +385,14 @@ class ShowMoreLess private constructor(builder: Builder) {
return this
}

/**
* @param labelBold is boolean to enable or disable bold label text
*/
fun labelBold(labelBold: Boolean): Builder {
this.labelBold = labelBold
return this
}

/**
* @param textClickableInCollapse for text collapse condition
* @param textClickableInExpand for text expand condition
Expand Down