vendredi 4 janvier 2019

Clikable chech Box text in android with Kotlin

I am creating an android app with kotlin. I have reached a point that I have an EULA (End User License Agreement) check box and I need to make a part of the text of the check box clikable in order to display the terms and conditions.

XML file

 <CheckBox
    android:id="@+id/accept_terms_and_conditions"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:text="I have read and accept the Terms &amp; Conditions"
    android:textColor="#000000"
    android:textSize="16sp"/>

I want to have the Teerms & Conditions part clicable.I have already tried the code below. This was initially java code found here and it was transformed to kotlin automaticaly by Android Studio but I didn't have much luck with it as the app crashes when the SignUpActivity starts.

Kotlin file

class SignUpActivity : AppCompatActivity() {

    private var mAuthTask: UserSignUpTask? = null


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


        val checkBox = findViewById<TextView>(R.id.accept_terms_and_conditions)

        val text = getText(R.string.terms_and_conditions)

        val ss = SpannableString(text)

        val clickableSpan1 = object : ClickableSpan() {

            override fun onClick(widget: View) {
                    Toast.makeText(this@SignUpActivity, "One", Toast.LENGTH_SHORT).show()
            }

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

        ss.setSpan(clickableSpan1, 28, 46, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)

        checkBox.text = ss
        checkBox.movementMethod = LinkMovementMethod.getInstance()
    }
}

So is there any way to do this from the XML file directly? And if not how can I do it in Kotlin?

Note: The API I am developing the app is 21.




Aucun commentaire:

Enregistrer un commentaire