I am referring to this documentation. I am getting unresolved reference errors for getSpans, getSpanStart, getSpanEnd, and it. What package do I have to import? I have already imported these:
import android.os.Bundle
import android.text.*
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
import androidx.core.content.ContextCompat
As another question, in general, how do people figure out what to import? Often times, it complains about unresolved reference like this and I find it difficult to find the right packages to import.
========================================================================
full code snippet
import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.*
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
import android.view.View
import android.widget.Toast.LENGTH_SHORT
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_login.*
import android.text.SpannableStringBuilder
class HtmlLink(val urlSpan: URLSpan, val spanStart: Int, val spanEnd: Int)
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val termsText = getText(R.string.terms)
val spannableString = SpannableString(termsText)
val annotations = termsText.getSpans(0, termsText.length, Annotation::class.java)
val clickableSpan = object : ClickableSpan() {
override fun onClick(widget: View) {
Snackbar.make(window.decorView.rootView, "URL is clicked", LENGTH_SHORT).show()
}
override fun updateDrawState(ds: TextPaint?) {
ds?.isUnderlineText = false
}
}
annotations?.find { it.value == "terms_link" }?.let {
spannableString.apply {
setSpan(
clickableSpan,
termsText.getSpanStart(it),
termsText.getSpanEnd(it),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
setSpan(ForegroundColorSpan(
ContextCompat.getColor(this@LoginActivity, R.color.colorAccent)),
termsText.getSpanStart(it),
termsText. getSpanEnd(it),
0
)
}
}
textView_terms.apply {
text = spannableString
movementMethod = LinkMovementMethod.getInstance()
}
}
}
You can use the getSpans method with a SpannableString object.
You are trying to apply it to termsText which is a String.
Change val annotations = termsText.getSpans... to:
val spannableString = SpannableString(termsText)
val annotations = spannableString.getSpans(0, termsText.length, Annotation::class.java)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With