Home > Back-end >  I can't get isChecked method working in Kotlin
I can't get isChecked method working in Kotlin

Time:04-26

Basically what the title says. I am trying to make a login page and I cant check the Remember the Password box. isChecked method gives Unresolved reference error. I tried setChecked() and get the same error.

Code that doesnt work:

nameremember_password.setOnClickListener(View.OnClickListener {

        if (!(nameremember_password.isSelected)) {
            nameremember_password.isChecked = true
            nameremember_password.isSelected = true
        } else {
            nameremember_password.isChecked = false
            nameremember_password.isSelected = false
        }
    })

This is activity_main.xml:

<RadioButton

    android:id="@ id/remember_password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@ id/login_button"
    android:layout_marginTop="40dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:text="Remember Password"
    android:textSize="12dp"
    android:checked="false"/>

CodePudding user response:

Here check the documentation -> https://developer.android.com/guide/topics/ui/controls/radiobutton

and you can change your function like this according to documentation

 nameremember_password.setOnClickListener { view ->
        if (view is RadioButton) {
            if (!(view.isSelected)) {
                view.isChecked = true
                view.isSelected = true
            } else {
                view.isChecked = false
                view.isSelected = false
            }
        }
    }
  • Related