1

I want to make "show more" in different colors but, it's not working using spannable in kotlin. please help.

                val mSpannableString = SpannableString("show more")
                val mBlue = ForegroundColorSpan(Color.BLUE)
                mSpannableString.setSpan(mBlue,2,7,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

                holder.MORE.text = "READ MORE"
                if (Expert_answer.length>=300) {

                    holder.description.text = Expert_answer.substring(0,300)+"..."+mSpannableString
                }else{
                    holder.description.text = Expert_answer
                }
9
  • please ask me anything related to this question Commented Dec 27, 2019 at 6:03
  • val mBlue = ForegroundColorSpan(Color.BLACK) so if you want it to be blue why are you using black? Commented Dec 27, 2019 at 6:05
  • 2
    just find how setSpan is used in java and convert it to kotlin, there is tons of examples on the net, one minute googling: developer.android.com/guide/topics/text/spans (btw they have both kotlin and java samples) Commented Dec 27, 2019 at 6:07
  • I can do it in java , but not working in kotlin. also, if any link you can share me that will be very helpful thanks. Commented Dec 27, 2019 at 6:10
  • i gave you a link, it has both kotlin and java samples Commented Dec 27, 2019 at 6:11

3 Answers 3

7

I have done this so I may be able to help you here.

   val txtShow ="show"
   val txtMore ="More"
   val spannable = SpannableString(txtShow) // String for which you want to change the color
   spannable.setSpan(ForegroundColorSpan(Color.RED), 0, txtShow.length, 
   Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
   txtMessage.text = TextUtils.concat(txtShow,txtMore)

Let me know if still any help require or stuck.Happy Coding!!!

Sign up to request clarification or add additional context in comments.

1 Comment

I was concating string using "+" only before loading to TextView , but using TextUtils.concat solve the problems thanks.
2

You can also create a custom implementation for the span as following.

 val spannable = SpannableStringBuilder(“Text is spantastic!”)
spannable.setSpan(
     ForegroundColorSpan(Color.RED), 
     8, 12, 
     Spannable.SPAN_EXCLUSIVE_INCLUSIVE)

For Your example here

 val showMore = "show more"
        val Expert_answer =
            "sample text  sample text sample text sample text sample text sample text sample text sample text "

        val Expert_answer =Expert_answer.substring(0, 30) + "..." + showmore
        val mSpannableString = SpannableStringBuilder(Expert_answer)

        val mBlue = ForegroundColorSpan(Color.BLUE)
        mSpannableString.setSpan(mBlue, 33, 42, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        abcTitle.text = mSpannableString

enter image description here

Happy Coding!!!

Comments

0

Below function will substring input to 300 characters (if it exceeds 300 chars), append "Show more" to the end of it and set value to holder.description.text

private fun setShowMoreIfNeeded(input: String) {
    val maxLenthOfStingShown = 300
    holder.description.text = if (input.length > maxLenthOfStingShown) {
        SpannableStringBuilder()
            .append(input.substring(0, maxLenthOfStingShown))
            .color(Color.BLUE) {append("Show more")}
    } else {
        input
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.