skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. 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());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search