0

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
2
  • This sounds like it might be the same issue here: stackoverflow.com/q/74118993. I have a couple of possible workarounds linked there. Commented Feb 3, 2023 at 13:44
  • No problem. Btw, if you had any issues getting those suggested solutions working, I've since updated them. The TextInputLayout class changed a bit, apparently. Just FYI. Cheers! Commented Feb 6, 2023 at 18:47

2 Answers 2

0

i don't see problems in your code, so, of course it sounds like a bug, but you can try:

  1. set focus.
  2. set text color.
  3. invoke invalidate() on text input layout in onBind viewholder method.

and see how the system reacts.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your comment. It turned out to be a bug on the component. Thanks to @Mike.M for the answer
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.