Android Set Horizontal Divider to a Vertical LinearLayout

(Last Updated On: November 3, 2017)

If you need to use a “list” divider, but you need it on a vertical LinearLayout instead of a List you can do this by using some attributes on the LinearLayout. It’s not very straightforward as you maybe expected, but it’s not very difficult either. Before starting, you have to know the 2 major things this tutorial will teach you:

  • how to add divider to a VERTICAL LinearLayout
  • how to add padding at the bottom between divider and content

Summary

  • create 2 drawables: one for shape and one for the actual drawable
  • add 2 LinearLayout attributes android:showDividers="end"
    and android:divider="@drawable/divider_drawable" for each LinearLayout

Now, let’s begin!

  1. Create a new project (activity_main.xml will be automatically generated. You will have to edit it later with the code from this tutorial).
  2. In the app – src – main – res – drawable folder,  create a new drawable and name it divider_shape.xml. Inside this xml we will have to create a rectangle shape with a hight of 1dp for the divider.

 3. Again, in the drawable folder, create another drawable xml and name it divider_drawable.xml. Inside this xml we have to create a layer-list which will use the drawable_shape.xml as drawable. Here is the place where you can handle the padding for the divider. In this tutorial we use top and bottom paddings.

4. Now in activity_main.xml, add the following code:

As you can see the “list” divider we want to simulate, uses the “end” value in order to be displayed at the bottom of the each linearLayout.

NOTE: Don’t forget to add the orientation attribute as VERTICAL for every LinearLayout. Otherwise, the default is Horizontal, and the divider won’t be displayed.

Android LinearLayout Dividers