Search code examples
androidandroid-recyclerviewdivider

DividerItemDecoration drawable draws fully opaque


No matter which way I create, set its alpha, or assign a drawable to DividerItemDecoration in my RecyclerView, the drawable is always rendered fully opaque in my view. Why is this and how can I get my divider to be transparent?

I've looked in the source for DividerItemDecoration and there's nowhere that the private mDivider field is having its alpha modified. It calls mDivider.draw(canvas), whose method comment says "Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter)."

I've tried creating the Drawable as an XML shape with a transparent color. I've tried creating the Drawable as a single pixel PNG. I've tried setting the alpha on the Drawable programmatically with setAlpha(). I've tried assigning the Drawable to the DividerItemDecoration programmatically with setDrawable(). I've tried assigning the Drawable in the theme with @android:attr/listDivider. All of these methods yield the same result. The Drawable is rendered fully opaque.


Solution

  • Ok, figured this out. The problem was the background color of the RecyclerView wasn't set. I was looking for the divider to draw on top of the item views, at the bottom of them. But it's actually drawing the divider view between the item views, with the RecyclerView color behind it (if set) or else the color of the view containing the RecyclerView. I was looking for the transparent color to show through the light color of the item view, instead it was showing through the default dark color of the screen behind the RecyclerView. Problem solved!