Skip to content

LinkMovementMethod with text gravity and singleLine issue #9

@2dxgujun

Description

@2dxgujun
private class LinkMovementMethodTouchListener : View.OnTouchListener {
    override fun onTouch(
      v: View,
      event: MotionEvent
    ): Boolean {
      val widget = v as TextView
      val action = event.action
      if (widget.text is SpannedString && action == MotionEvent.ACTION_UP) {
        var x = event.x.toInt()
        var y = event.y.toInt()
         x -= widget.totalPaddingLeft
        y -= widget.totalPaddingTop
         x += widget.scrollX
        y += widget.scrollY
         val layout = widget.layout
        val line = layout.getLineForVertical(y)
        val off = layout.getOffsetForHorizontal(line, x.toFloat())
         val links = (widget.text as SpannedString).getSpans(off, off, ClickableSpan::class.java)
         if (links.isNotEmpty()) {
          links[0].onClick(widget)
          return true
        }
      }
      return true
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions