0

here is sample code

val span = SpannableString("test test test simple text ")

span.setSpan(object : ClickableSpan() {
    override fun onClick(v: View) {
         Log.i("onClick", "span")
        }
    }, 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

editTv.text = span
editTv.setOnClickListener {
     Log.i("onClick","tv")
}

editTv.setMovementMethod(LinkMovementMethod.getInstance());

in this, when I select the clickable span(5~9) react both the onclick of text view and the onclick of the span... how can I get response only span's onclick???

Bc Lee
  • 44
  • 1
  • 1
  • 9
  • basically you are making textview as a span so you have to add one click on it becuase that textview is spanned text. so you should add only span click – unzila Jun 24 '19 at 03:51

2 Answers2

0

Is there a way that only span's onclick can react?

Then you can put a condition where a span's onclick will react to it.

If you have both textviews onClickListener and Span Click listener. then both function will run based on what they are been design.

KikX
  • 217
  • 2
  • 17
0

Well, since you want both the onClicks to work for their designated tap areas, what you can do is implement multiple ClickableSpan and remove the general tv.setOnClickListener you set in the end.

Something like this:

Let's say for example your string is : "word1 word2 word3 word4"

ClickableSpan1 will handle onClick for word1

ClickableSpan2 will handle onClick for word2 and word3

ClickableSpan3 will handle onClick for word4

Check the below for example code. You can also style each Clickable spans accordingly : https://stackoverflow.com/a/22006998/2607144

Azhar92
  • 923
  • 2
  • 8
  • 17