Search code examples
androidkotlinandroid-recyclerviewrx-kotlindata-class

Android line divider


Learning android with a two friends and right now we're focusing on just UI portion. One friend gave us the task of creating a display with dashed line divider. We're trying to add it in the Adapter file so it can be loaded in our fragment as part of the recycler view but can't figure out how.

Here's the layout in where the dotted line drawable gets passed in

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/dashes"
        android:background="@drawable/dashes_line"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

Solution

  • this is a way you can set line divider for RecyclerView

    recyclerView.addItemDecoration(DividerItemDecoration(context, VERTICAL).apply {
               setDrawable(getDrawable(context, R.drawable.separator))
        })
    

    You can change the drawable as you see fit.

    Update: add drawable separator.xml

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="line">
      <size android:height="3dp"/>
      <stroke
          android:color="#000000"
          android:dashWidth="10px"
          android:dashGap="10px"
          android:width="1dp"/>
    </shape>