1

I got a NavigationView which I'm trying to set a custom list divider to.

I have made the file "drawer_list_divider.xml":

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle"
android:thickness="25dp">
<solid android:color="@color/secondaryBackground"/>

I set it like this: android:theme="@style/NavigationViewTheme"

The style:

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

The divider gets the color I want, but not the thickness which is not affected. I want the divider to be a rectangle of a different height.. how do I set the height/thickness?

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

2 Answers2

0

Navigation Drawer layout

<android.support.v4.widget.DrawerLayout  
 android:id="@+id/drawerLayout"  
 android:layout_width="match_parent"  
 android:layout_height="match_parent"
 android:layout_below="@+id/appBar">  
 <!--Content-->  
 <LinearLayout  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical">  
   <TextView  
     android:id="@+id/content_textView"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_gravity="center"  
     android:text="@string/app_name" />  
 </LinearLayout>  
 <!-- Navigation Drawer-->  
 <android.support.v7.widget.RecyclerView  
   android:id="@+id/drawerRecyclerView"  
   android:layout_width="300dp"  
   android:layout_height="match_parent"  
   android:layout_gravity="left"  
   android:background="#ffffff">  
 </android.support.v7.widget.RecyclerView>  

Then set a recyclerview adapter

   public class DrawerAdapter extends 
  RecyclerView.Adapter<DrawerAdapter.DrawerViewHolder> {  
 private ArrayList<DrawerItem> drawerMenuList;  
   public DrawerAdapter(ArrayList<DrawerItem> drawerMenuList) {  
 this.drawerMenuList = drawerMenuList;  
 }  
@Override  
public DrawerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
 View view;  
 view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false);  
 return new DrawerViewHolder(view);  
}  
 @Override  
 public void onBindViewHolder(DrawerViewHolder holder, int position) {  
 holder.title.setText(drawerMenuList.get(position).getTitle());  
 holder.icon.setImageResource(drawerMenuList.get(position).getIcon());  
 }  
 @Override  
 public int getItemCount() {  
 return drawerMenuList.size();  
 }  
 class DrawerViewHolder extends RecyclerView.ViewHolder {  
 TextView title;  
 ImageView icon;  
 public DrawerViewHolder(View itemView) {  
   super(itemView);  
   title = (TextView) itemView.findViewById(R.id.title);  
   icon = (ImageView) itemView.findViewById(R.id.icon);  
 }      }      }  

Then set the adapter were you want to show the navigation drawer. While setting the recyclerview set the divider

 DrawerAdapter adapter = new DrawerAdapter(mDrawerItemList);  
 mRecyclerView.setLayoutManager(new LinearLayoutManager(this));  
 mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
 mRecyclerView.setAdapter(adapter);  

Here is the itemDecorator class

public class SimpleDividerItemDecoration extends RecyclerView.ItemDecoration {
private Drawable mDivider;

public SimpleDividerItemDecoration(Context context) {
    mDivider = context.getResources().getDrawable(R.drawable.recycler_horizontal_divider);
}

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    int left = parent.getPaddingLeft();
    int right = parent.getWidth() - parent.getPaddingRight();

    int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View child = parent.getChildAt(i);

        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

        int top = child.getBottom() + params.bottomMargin;
        int bottom = top + mDivider.getIntrinsicHeight();

        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}}

Here is the divider drawable.

  <?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="#2EC590" />

</shape>
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
  • 1
    I will try it soon, but before that, do you think there's a way to achieve the same result with a NavigationView instead of a RecyclerView? – CodeMonkey Jul 31 '17 at 07:25
  • without using recyclerview or any other list views , i don;t think you can increase the divider width. – Manoj Perumarath Jul 31 '17 at 07:37
  • OK you can this works, but I'm just missing something.. Is there a way to create groups of drawer items like you could with a navigation view and add the divider only between each group and not between each item? – CodeMonkey Jul 31 '17 at 09:10
  • you can customize the recyclerview so that it populate multiple layouts at runtime. :) – Manoj Perumarath Jul 31 '17 at 09:27
  • What do you mean? If you set the shape divider as the decoration, how can it know when a group of item ends? – CodeMonkey Jul 31 '17 at 10:03
  • I'd recommend convincing your designer (which may be yourself :) ) that it's not worth it to reinvent the wheel and to recode a side menu from scratch, just to be able to specify the height of the divider. What value does this really give the app? Think about the cost to maintain this. Much better to stick to the standard components when possible. Of course if you're already customizing the heck out of the side menu, adding an item decorator is just one more addition. But doing all this JUST to change the separator height? – Carmen May 06 '20 at 16:52
0

You should use the tag size rather than thickness in the tag shape.

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >

    <size android:height="1dp" />

    <solid android:color="#10000000"/>

</shape>
Bob
  • 13,447
  • 7
  • 35
  • 45