From 42c2a2f72a1f104d2d6e2c5fcd61e89a7f7735d9 Mon Sep 17 00:00:00 2001 From: johan12345 Date: Wed, 26 Feb 2025 21:20:15 +0100 Subject: [PATCH] improve setLinkify BindingAdapter fixes #371 --- .../net/vonforst/evmap/ui/BindingAdapters.kt | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/net/vonforst/evmap/ui/BindingAdapters.kt b/app/src/main/java/net/vonforst/evmap/ui/BindingAdapters.kt index d5eeed5e..83b0c2f6 100644 --- a/app/src/main/java/net/vonforst/evmap/ui/BindingAdapters.kt +++ b/app/src/main/java/net/vonforst/evmap/ui/BindingAdapters.kt @@ -6,8 +6,9 @@ import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.graphics.drawable.LayerDrawable -import android.text.SpannableString import android.text.format.DateUtils +import android.text.method.LinkMovementMethod +import android.text.util.Linkify import android.view.View import android.view.ViewGroup.MarginLayoutParams import android.widget.ImageView @@ -215,21 +216,25 @@ fun setTopMargin(view: View, topMargin: Float) { /** * Linkify is already possible using the autoLink and linksClickable attributes, but this does not - * remove spans correctly. So we implement a new version that manually removes the spans. + * remove spans correctly after autoLink is set to false. + * So we implement a new version that manually uses Linkify to create links if necessary. */ -@BindingAdapter("linkify") -fun setLinkify(textView: TextView, oldValue: Int, newValue: Int) { - if (oldValue == newValue) return +@BindingAdapter(value = ["linkify", "android:text"]) +fun setLinkify( + textView: TextView, + oldLinkify: Int, + oldText: CharSequence?, + newLinkify: Int, + newText: CharSequence? +) { + if (oldLinkify == newLinkify && oldText == newText) return - textView.autoLinkMask = newValue - textView.linksClickable = newValue != 0 - - // remove spans - val text = textView.text - if (newValue == 0 && text != null && text is SpannableString) { - text.getSpans(0, text.length, Any::class.java).forEach { - text.removeSpan(it) - } + textView.text = newText + if (newLinkify != 0) { + Linkify.addLinks(textView, newLinkify) + textView.movementMethod = LinkMovementMethod.getInstance() + } else { + textView.movementMethod = null } }