If you are using ListView, you can certainly follow Daryl's answer. However, the divider will be there even after the last item. I will certainly suggest switching to RecyclerView which is an advance version of ListView with more power and features. In RecyclerView, do it like this:
Create a line diveder drawable. (Change the size and color according to your need).
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="4dp"
android:height="4dp"/>
<solid android:color="#d5d5d5" />
Create a DividerItemDecorator class.
public class DividerItemDecorator extends RecyclerView.ItemDecoration {
private Drawable mDivider;
public DividerItemDecorator(Context context) {
mDivider = ContextCompat.getDrawable(context, R.drawable.line_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);
}
}
}
Finally, set the DividerItemDecorator in your Recycler View using this line of code :
recyclerView.addItemDecoration(new DividerItemDecorator(getApplicationContext()));