Created
August 29, 2017 22:17
-
-
Save NikolaDespotoski/0626017263f80c450a1a719476a688ae to your computer and use it in GitHub Desktop.
Open clicked links of TextView in CustomTabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomTabsURLSpan(url: String) : URLSpan(url) { | |
override fun onClick(widget: View?) { | |
// do something with url | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Rect | |
import android.text.Spannable | |
import android.text.Spanned | |
import android.text.TextUtils | |
import android.text.method.TransformationMethod | |
import android.text.style.URLSpan | |
import android.text.util.Linkify | |
import android.view.View | |
import android.widget.TextView | |
class LinkTransformationMethod : TransformationMethod { | |
override fun onFocusChanged(p0: View?, p1: CharSequence?, p2: Boolean, p3: Int, p4: Rect?) { | |
} | |
override fun getTransformation(source: CharSequence?, textView: View?): CharSequence { | |
if (textView is TextView && TextUtils.isEmpty(source) && textView.text !is Spannable) { | |
Linkify.addLinks(textView, Linkify.WEB_URLS) | |
val text: Spannable = textView.text as Spannable | |
text.getSpans(0, textView.length(), URLSpan::class.java).forEach { span: URLSpan -> | |
val spanStart = text.getSpanStart(span) | |
val spanEnd = text.getSpanEnd(span) | |
text.removeSpan(span) | |
text.setSpan(CustomTabsURLSpan(span.url), spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | |
} | |
return text | |
} | |
return source!! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment