Search code examples
androidkotlinandroid-edittextimageview

View.VISIBLE and INVISIBLE doesn't work Kotlin


I have edittext for password and button to show password. Button should become visible only when edittext isn't empty. I made some code, but it isn't working (it is always invisible). Here is code:

passwordEditTextSUA.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(s: Editable?) {}
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            if (passwordEditTextSUA.text.toString() != "") {
                showPasswordSUA.visibility == View.VISIBLE
            } else {
                showPasswordSUA.visibility == View.INVISIBLE
            }
        }
    })

And xml of button and edittext:

<EditText
        android:id="@+id/passwordEditTextSUA"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="34dp"
        android:layout_marginEnd="34dp"
        android:layout_marginBottom="35dp"
        android:hint="@string/enter_password"
        android:inputType="textPassword"
        android:theme="@style/EditTextTheme"
        app:layout_constraintBottom_toTopOf="@+id/repeatPasswordEditTextSUA"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

<ImageView
        android:id="@+id/showPasswordSUA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="1dp"
        android:background="@android:color/transparent"
        android:src="@drawable/button_show_password"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="@+id/passwordEditTextSUA"
        app:layout_constraintEnd_toEndOf="@+id/passwordEditTextSUA"
        app:layout_constraintTop_toTopOf="@+id/passwordEditTextSUA" />

What's wrong?


Solution

  • replace this showPasswordSUA.visibility == View.VISIBLE with showPasswordSUA.visibility = View.VISIBLE I work with java but I think the double equal sign simply evaluates to true or false. you want to use just one =. see this