Result

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        tools:context=".MainActivity">

    <TextView
            android:id="@+id/textView"
            android:textSize="20sp"
            android:textAlignment="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:background="@android:color/white"
            android:textColorHighlight="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

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

        val styledString = SpannableString(
            "By continue, you agree to the Terms of Service and Privacy Policy"
        )

        val clickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                // We display a Toast. You could do anything you want here.
                Toast.makeText(this@MainActivity, "Clicked", Toast.LENGTH_SHORT).show()
            }
        }

        styledString.setSpan(clickableSpan, 30, 46, 0)

        // make text italic
        styledString.setSpan(StyleSpan(Typeface.ITALIC), 51, 65, 0)
        styledString.setSpan(StyleSpan(Typeface.BOLD), 51, 65, 0)

        // make text bold
        styledString.setSpan(StyleSpan(Typeface.BOLD), 30, 46, 0)

        // underline text
        styledString.setSpan(UnderlineSpan(), 30, 46, 0)

        // add strike through span
        styledString.setSpan(StrikethroughSpan(), 0, 11, 0)

        // change text color
        styledString.setSpan(ForegroundColorSpan(Color.RED), 30, 46, 0)

        // highlight text
        styledString.setSpan(BackgroundColorSpan(Color.WHITE), 30, 46, 0)

        // this step is mandated for the url and clickable styles.
        textView.movementMethod = LinkMovementMethod.getInstance()

        textView.text = styledString
    }
}

 


Leave a Reply

Your email address will not be published. Required fields are marked *