Search code examples
androidandroid-recyclerviewandroid-textinputlayout

Android TextInputLayout hint gets bugged when recycled by a RecyclerView


USE CASE

I have a RecyclerView whose ViewHolder contains a TextInputLayout. For some situations I want it to display a hint and for other situations I don't.

ISSUE

When the RecyclerView recycles a ViewHolder that has previously displayed a hint but on the new bind does not require a hint. It will display an empty hint instead. See picture:

Image for NOT recycled ViewHolder

Image for recycled ViewHolder

The problem seems to happen ONLY if the new binding has NO hint. If I set a hint to every ViewHolder with different values, it recycles and displays correctly.

Snippet

Adapter.kt

override fun onBindViewHolder(holder: FormViewHolder, position: Int) {
    holder.onBind(getItem(position))
}

ViewHolder.kt

override fun onBind(model: Model) {
    binding.inputLayout.isHintEnabled = model.hasHint
    binding.inputLayout.isHintAnimationEnabled = model.hasHint
    binding.inputLayout.hint = model.hintValue
}

I tried:

  • Updating to latest version ATM: com.google.android.material:material:1.8.0

Solution

  • TextInputLayout will only recalculate the cutout dimensions if the hint is enabled and not empty, so as soon as we try to set either of those values, it can never reach the method necessary to close the cutout. The simplest straightforward solution might just be to create a new TextInputLayout/EditText instance whenever you need to clear the hint. Otherwise, we can do something with reflection

    Is there a way to redraw a TextInputLayout’s OutlinedBox?

    Thanks to @Mike.M for the answer