thieumao

See More TextView - Kotlin

Jan 29th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.51 KB | None | 0 0
  1.     private val showMoretext = "... See More"
  2.     private val maxVisibleText = 100
  3.     private val maxLine = 5
  4.  
  5.  
  6.     fun makeTextViewResizable(tv: TextView, maxLine: Int, expandText: String, comment: String) {
  7.         tv.text = comment
  8.         val vto = tv.viewTreeObserver
  9.         vto.addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
  10.  
  11.             override fun onGlobalLayout() {
  12.                 val text: String
  13.                 val lineEndIndex: Int
  14.                 val obs = tv.viewTreeObserver
  15.                 obs.removeGlobalOnLayoutListener(this)
  16.                 if (maxLine == 0) {
  17.                     lineEndIndex = tv.layout.getLineEnd(0)
  18.                     text = tv.text.subSequence(0, lineEndIndex - expandText.length + 1).toString() + " " + expandText
  19.                 } else if (maxLine > 0 && tv.lineCount >= maxLine) {
  20.                     lineEndIndex = tv.layout.getLineEnd(maxLine)
  21.                     val subStringLength = minOf(lineEndIndex, maxVisibleText)
  22.                     text = tv.text.subSequence(0, subStringLength).toString() + " " + expandText
  23.                 } else {
  24.                     lineEndIndex = tv.layout.getLineEnd(tv.layout.lineCount - 1)
  25.                     val subStringLength = minOf(lineEndIndex, maxVisibleText)
  26.                     text = tv.text.subSequence(0, subStringLength).toString() + " " + expandText
  27.                 }
  28.                 tv.text = text
  29.                 tv.movementMethod = LinkMovementMethod.getInstance()
  30.             }
  31.         })
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment