I would like to locate a view just after the last word of a (potentially) multi-line TextView.
For example, the TextView might be two lines, with the second line being about half as long as the TextView's width. How do I locate a View just after the last word.
I also want to attach a click listener on that Icon. I am using data binding.
I tried spannabale strings.
I achieved this functionality.
Have a look
@BindingAdapter("yourTAG")
fun setText(textView: TextView, text: SpannableString?) {
textView.movementMethod = LinkMovementMethod.getInstance()
if (!text.isNullOrBlank()) {
textView.text = text
}
Then in your viewModel create this function
private fun getSpannable(text:CharSequence): SpannableString{
val icon: Drawable = resourcesService.getDrawable(R.drawable.your_icon)
val spannableString = SpannableString(text)
val position = spannableString.length-1
icon.setBounds(0, 0, icon.intrinsicWidth, icon.intrinsicHeight)
val imageSpan = ImageSpan(icon, ImageSpan.ALIGN_BASELINE)
spannableString.setSpan(imageSpan, position, position +1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
val clickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
//TODO
}
}
spannableString.setSpan(clickableSpan, position, position + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
return spannableString
}
var myField = getSpannable("Thank you Stackoverflow")
In your view
app:yourTAG="@{vm.myField}"
}