71

I'm trying to set my custom drawable (line) for DividerItemDecoration, but with no success. Where is the mistake?

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),
                LinearLayoutManager.VERTICAL);
dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.sk_line_divider));

XML shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="1dp"
        android:color="#000000">
    </stroke>
</shape>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user3352926
  • 1,017
  • 1
  • 10
  • 15
  • 14
    By the way, you don't need `DividerItemDecoration#setDrawable`, you can set `android:listDivider` attribute in your theme instead. – arekolek Nov 02 '17 at 10:48

6 Answers6

102

Change the shape to rectangle.

Ex:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <size
        android:width="1dp"
        android:height="1dp" />
    <solid android:color="@color/primary" />
</shape>
Jonathan Vicente
  • 1,136
  • 1
  • 9
  • 3
11

Programmatic (Solution):

If you just want to change the color for the dividers instead of creating a custom drawable you can use a ColorDrawable:

DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
itemDecoration.setDrawable(new ColorDrawable(R.color.greycc));
recyclerView.addItemDecoration(itemDecoration);

If the size matters in addition to colors you can use a GradientDrawable:

DividerItemDecoration itemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);

GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[]{0xfff7f7f7, 0xfff7f7f7});
drawable.setSize(1,1);
itemDecoration.setDrawable(drawable);

recyclerView.addItemDecoration(itemDecoration);

Note that setting the color values in the array requires a full octet of hex values, otherwise incorrect colors will be shown i.e., 0xFF3E3E3E as opposed to 0X3E3E3E.

Colored Dividers Update (2023):

val itemDecoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
itemDecoration.setDrawable(
ResourcesCompat.getDrawable(resources, android.R.color.holo_green_light, theme)!!
)
recyclerView.addItemDecoration(itemDecoration)
6rchid
  • 1,074
  • 14
  • 19
8

If you want to change divider color, you can change it from you AppTheme by adding this line
<item name="android:listDivider">@color/your_color</item>

Raed
  • 844
  • 11
  • 10
2

Thanks to arekolek, I didn't know there's an API like this for android:listDivider.

But, there's a few way you can customise the DividerItemDecoration.

You'll need a drawable for the customised separator.

bg_border_grey.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="1dp"
        android:height="1dp" />
    <solid android:color="@color/borderGrey" />
</shape>

Method #1 - Theme

If you want apply theme wide, you can just add an attribute. Then, it will propagate down the children with the same theme.

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:listDivider">@drawable/bg_border_grey</item>
</style>

Method #2 - Programmatically

If you want to just customise the DividerItemDecoration itself and directly. Just setDrawable and you'll be right as rain.

val dividerItemDecoration = DividerItemDecoration(
    binding.recyclerView.context,
    DividerItemDecoration.VERTICAL
)
dividerItemDecoration.setDrawable(context?.getDrawable(R.drawable.bg_border_grey))
binding.recyclerView.addItemDecoration(dividerItemDecoration)
Morgan Koh
  • 2,297
  • 24
  • 24
1
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(),
                LinearLayoutManager.HORIZONTAL);
dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.line_decoration));
        recyclerView.addItemDecoration(dividerItemDecoration);
        DividerItemDecoration dividerItemDecorationVertical = new DividerItemDecoration(getContext(),
                LinearLayoutManager.VERTICAL);

        dividerItemDecorationVertical.setDrawable(getContext().getResources().getDrawable(R.drawable.line_decoration));
        recyclerView.addItemDecoration(dividerItemDecorationVertical);
4b0
  • 21,981
  • 30
  • 95
  • 142
0

May it help to someone.

Based on @Morgan-Koh, Created ShapeDrawable programmatically

    val decoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
    //decoration.setDrawable(ColorDrawable(Color.WHITE))
    val shapeDrawable = ShapeDrawable()
    shapeDrawable.paint.color = Color.WHITE
    shapeDrawable.intrinsicHeight = 2
    shapeDrawable.intrinsicWidth = resources.displayMetrics.widthPixels / resources.displayMetrics.densityDpi
    decoration.setDrawable(shapeDrawable)
    recyclerView?.addItemDecoration(decoration)
Sumit
  • 1,022
  • 13
  • 19