0

I'm having an issue using Material button on a recycler view. When i set iconGravity = ICON_GRAVITY_TEXT_START looks like it's property not work sometimes when view is recycled.

I notice that when all rows have an icon, this problem not occurs, but when one row not show an icon i got this case.

The xml i'm using

<com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:background="@android:color/holo_red_dark"
        android:minWidth="160dp"
        app:backgroundTint="@null"
        android:gravity="center"
        app:iconGravity="textStart"
        app:iconPadding="8dp"
        android:textAlignment="center" />

And the code

fun bind(data: ButtonVO) {
    button.apply {
        text = data.text
        if(data.showIcon) 
            setIconResource(R.drawable.ic_play_arrow)
        else
            setIconResource(0)
    }
}

The result when view is recycled

enter image description here

Someone have any clue?

  • "looks like it's property not work sometimes when view is recycled" What exactly is the problem? What would you expect to have? And where is the code for when you want the button to have no image? – Adinia Feb 24 '22 at 14:04
  • The problem is the gravity. In the image, on button 34 you can see the padding of the icon different from the others buttons – Rafael Godinho Brandão Feb 24 '22 at 14:09
  • 1
    Ah, ok, I see it now, but you should really put that in the question itself, it's not at all evident what is the problem. Also, it'd be good to add a [mcve]. – Adinia Feb 24 '22 at 14:20
  • 1
    Try adding an else clause for `data.showIcon` removing the icon (icon = null) when it's not needed. For recycler view you always have to add both the clauses otherwise the recycling is always unpredictable. – Talha Mir Feb 24 '22 at 14:34
  • Does this answer your question? [Android: RecyclerView content messed up after scrolling](https://stackoverflow.com/questions/29702357/android-recyclerview-content-messed-up-after-scrolling) – Adinia Feb 24 '22 at 14:37
  • hi @TalhaMir. I update the question...i already tried it before without success – Rafael Godinho Brandão Feb 24 '22 at 15:26

0 Answers0