Checkbox Issue on Android Kotlin

67 Views Asked by At

I have a checkbox with link in its text .

When ever I checked the checkbox with touch , it automatically clicks the link also which is present in its text . I have used spannable text for making part of text as link . This is causing issue .

How can I resolve it .

2

There are 2 best solutions below

0
龚诗豪 On

Here is a simplest method. You can create a new view whose size equal the checkbox and place it over the checkbox. What's more, the view that you have just created is attached by a click listner in which you set the checkbox's state manually to trigger the visual change of the checkbox.

2
Saurav Suman On

you can find the xml layout below for checkbox with text which has a link in the string file. You can set the link dynamically as well.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/text_with_link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text_with_link_string" />
<string name="text_with_link_string">I agree to the <a href="https://example.com/">terms and conditions</a></string>

If you need the layout in compose. I can add it as well. Please let me know if this solves your problem.