Skip to content

LinkMovementMethod with text gravity and singleLine issue #9

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
    }
  }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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