0

I want to create a TextView with just a few words clickable. When user clicks on that link a Webview is created.

I also would like to change the color of the link as well.

In HTML I can do this:

<p>By checking this box, I acknowledge that I have reviewed the <a href="To the other page"> Online Payment Terms & Conditions</a> and agree.</p>

How do I create this in Android studio?

What I have so far:

  • Layout XML:
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/terms_condition_message"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintEnd_toEndOf="parent" />
  • Strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
     <string name"terms_condition_message"><![CDATA[By checking this box, I acknowledge that I have reviewed the %1$s<color="#fff3670b4">Online Payment Terms & Conditions</color>%2$s and agree.
Nimantha
  • 6,405
  • 6
  • 28
  • 69
J Single
  • 21
  • 4
  • Does this answer your question? [How to make links in a TextView clickable](https://stackoverflow.com/questions/2734270/how-to-make-links-in-a-textview-clickable) – AgentP Jan 02 '22 at 14:14
  • @AgentP. It helps but the issue for me is that I only want to link part of the string not the whole string such as that question. – J Single Jan 03 '22 at 16:50

2 Answers2

1

Add android:linksClickable="true" and android:textColorLink="@color/colorLink" in your text view.

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/terms_condition_message"
  app:layout_constraintTop_toTopOf="parent"
  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintEnd_toEndOf="parent"
  android:linksClickable="true"
  android:textColorLink="@color/colorLink" />

Set movementMethod to TextView in Java or Kotlin class.

In Kotlin:

textView.movementMethod = LinkMovementMethod.getInstance()

In Java:

textView.setMovementMethod(LinkMovementMethod.getInstance());
Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

Here is how I did it.

TextView:

  <TextView
        android:id="@+id/forgot_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Forgot Password"
        android:textSize="20sp"
        app:layout_constraintBottom_toTopOf="@id/sign_up_prompt"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/sign_up_prompt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/forgot_password" />

Main Activity:


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val forgotPasswordTextView = findViewById<TextView>(R.id.forgot_password)

        forgotPasswordTextView.setOnClickListener {
            val myIntent = Intent(this, ForgotPasswordActivity::class.java)
            startActivity(myIntent)

            forgotPasswordTextView.movementMethod = LinkMovementMethod.getInstance()
        }

        /*************************************************/

        val ss = SpannableString("By checking this box, I acknowledge that I have reviewed the Online Payment Terms & Conditions.")
        val clickableSpan: ClickableSpan = object : ClickableSpan() {
            override fun onClick(textView: View) {
                startActivity(Intent(this@MainActivity, SignUpActivity::class.java))
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.setColor(Color.BLUE)
                ds.isUnderlineText = true
            }
        }

        ss.setSpan(clickableSpan, 61, 94, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        val boldSpan = StyleSpan(Typeface.NORMAL)
        ss.setSpan(boldSpan, 61, 94, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        val textView = findViewById<TextView>(R.id.sign_up_prompt)
        textView.text = ss
        textView.movementMethod = LinkMovementMethod.getInstance()
        textView.highlightColor = Color.TRANSPARENT
    }
}

Blog that I used to help: https://medium.com/@sairamravuri/clickable-textview-in-kotlin-a242f7168b89

Nimantha
  • 6,405
  • 6
  • 28
  • 69
J Single
  • 21
  • 4