Add (or amend) your values/attrs.xml to include:


  
    
    
    
  

Some layout:




  

  


And the class:

package com.qozix.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.qozix.widgets.R;


/**
 * Created by michaeldunn on 8/15/16.
 */
public class SeparatorLinearLayout extends LinearLayout {

  private Drawable mSeparator;
  private Rect mSeparatorBounds = new Rect();
  private int mSeparatorHeight;
  private int mSeparatorWidth;

  public SeparatorLinearLayout(Context context) {
    this(context, null);
  }

  public SeparatorLinearLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public SeparatorLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setWillNotDraw(false);
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SeparatorLinearLayout, 0, 0);
    try {
      Drawable drawable = typedArray.getDrawable(R.styleable.SeparatorLinearLayout_separator);
      if (drawable != null) {
        setSeparator(drawable);
      }
      int drawableWidth = typedArray.getInt(R.styleable.SeparatorLinearLayout_separatorWidth, 0);
      if (drawableWidth != 0) {
        setSeparatorWidth(drawableWidth);
      }
      int drawableHeight = typedArray.getInt(R.styleable.SeparatorLinearLayout_separatorHeight, 0);
      if (drawableHeight != 0) {
        setSeparatorHeight(drawableHeight);
      }
    } finally {
      typedArray.recycle();
    }
  }

  public void setSeparator(Drawable drawable) {
    mSeparator = drawable;
    if (mSeparator != null) {
      mSeparatorHeight = mSeparator.getIntrinsicHeight();
      mSeparatorWidth = mSeparator.getIntrinsicWidth();
    }
  }

  public void setSeparatorHeight(int separatorHeight) {
    mSeparatorHeight = separatorHeight;
  }

  public void setSeparatorWidth(int separatorWidth) {
    mSeparatorWidth = separatorWidth;
  }

  private void drawVerticalSeparators(Canvas canvas) {
    mSeparatorBounds.left = 0;
    mSeparatorBounds.right = canvas.getWidth();
    for (int i = 0; i < getChildCount() - 1; i++) {
      View child = getChildAt(i);
      mSeparatorBounds.top = child.getBottom() - 1;
      mSeparatorBounds.bottom = mSeparatorBounds.top + mSeparatorHeight;
      drawDrawableToCanvas(canvas);
    }
  }

  private void drawHorizontalSeparators(Canvas canvas) {
    mSeparatorBounds.top = 0;
    mSeparatorBounds.bottom = canvas.getHeight();
    for (int i = 0; i < getChildCount() - 1; i++) {
      View child = getChildAt(i);
      mSeparatorBounds.left = child.getRight() - 1;
      mSeparatorBounds.right = mSeparatorBounds.left + mSeparatorWidth;
      drawDrawableToCanvas(canvas);
    }
  }

  private void drawDrawableToCanvas(Canvas canvas) {
    mSeparator.setBounds(mSeparatorBounds);
    mSeparator.draw(canvas);
  }

  private void drawSeparators(Canvas canvas) {
    switch (getOrientation()) {
      case VERTICAL:
        drawVerticalSeparators(canvas);
        break;
      case HORIZONTAL:
        drawHorizontalSeparators(canvas);
        break;
    }
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mSeparator != null) {
      drawSeparators(canvas);
    }
  }
}