Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set drawable for DividerItemDecoration

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> 
like image 273
user3352926 Avatar asked Nov 10 '16 12:11

user3352926


People also ask

How do you set the color in DividerItemDecoration?

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.

What is DividerItemDecoration?

DividerItemDecoration is a RecyclerView. ItemDecoration that can be used as a divider between items of a LinearLayoutManager . It supports both HORIZONTAL and VERTICAL orientations.

How do I add a divider to my recycler view?

We have to create a default Divider using addItemDecoration() method with the RecyclerView instance, we need to pass the ItemDecoration(in this case it is DividerItemDecoration()) instance and the orientation of the LayoutManager(in this case it is vertical) of the recycler view.


2 Answers

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> 
like image 183
Jonathan Vicente Avatar answered Sep 23 '22 04:09

Jonathan Vicente


Solution (Programmatically):

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 GradientDrawwable:

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); 

Please 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.

like image 31
6rchid Avatar answered Sep 23 '22 04:09

6rchid