2

I have some text with 2 links like: "Please read and accept our Terms and Privacy Policy ...."

<string name="terms_and_policy">... <a href='https://docs.google.com/document/d/xxx'><font color="#FC672B">**Terms**</font></a> and <a href='https://docs.google.com/document/d/xxx'><font color="#FC672B">**Privacy Policy**</font></a> ...</string>

Possible handle 2 links use this case, when have 2 links in one sentense or need use another approach?

P.S. I need catch separate 2 click to post event for Analytics.

nicolas asinovich
  • 3,201
  • 3
  • 27
  • 37

1 Answers1

2

I have used as below mentioned in my apps.

string.xml

<string name="about_fragment_privacy_policy" translatable="false">User Agreement and Privacy Policy</string>

Layout.xml

<TextView
    android:id="@+id/privacyPolicy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/about_fragment_privacy_policy" />

Kotlin code

string = getString(R.string.about_fragment_privacy_policy)
spannableString = SpannableString(string)
val clickableUserAgreement = object : ClickableSpan() {
    override fun onClick(widget: View) {
        startActivity(
            Intent(Intent.ACTION_VIEW).setData(
                Uri.parse(
                    "https://example.com"
                )
            )
        )
    }
}
val clickablePrivacyPolicy = object : ClickableSpan() {
    override fun onClick(widget: View) {
        startActivity(
            Intent(Intent.ACTION_VIEW).setData(
                Uri.parse(
                    "https://example.com"
                )
            )
        )
    }
}
spannableString.setSpan(
    clickableUserAgreement,
    0,
    14,
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
    ForegroundColorSpan(resources.getColor(R.color.colorPrimary)),
    0,
    14,
    0
)
spannableString.setSpan(
    clickablePrivacyPolicy,
    19,
    string.length,
    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
spannableString.setSpan(
    ForegroundColorSpan(resources.getColor(R.color.colorPrimary)),
    19,
    string.length,
    0
)
privacyPolicy.text = spannableString
privacyPolicy.movementMethod = LinkMovementMethod.getInstance()
Dilanka Laksiri
  • 408
  • 3
  • 12