Android draw a line in layout

You can add the divider through xml itself. But with RecyclerView, you can not directly add a divider. You need to add an ItemDecoration which can draw the divider. But developers find it difficult and directly add the dividers to the view, instead of using the item decoration.

Whenever we are taking a shortcut it might have some adverse effects. In this case it affects performance.

XML attributes

When adding the divider to the layout, we are increasing the view count. We know that having less number of views in the layout is better for performance.


  1. bitstrips free download for blackberry.
  2. How to Add a horizontal line in Android Layout.
  3. Popular Posts.
  4. How to add a dashed line in an Android layout!
  5. Method two – Specifying a divider for the LinearLayout?
  6. Method one – Manually adding a view separator to the LinearLayout.

Sometimes adding the divider as a view increases the layout hierarchy. To add a divider we had to create an additional outer layout.

How to create simple view separators

During the item animation divider will be animated along with the animation, since divider is part of the view. To get more clarity, look at the following gif. Clearly dividers should not be animated along with the item view. In case you want to do just more than visibility, item decorations are flexible.

Source Code :

In the above image for the last item in the group divider fills the entire width. Other dividers have a margin of 56dp to their left side. Writing your own item decoration is simple. You just need to create a class which extends from ItemDecoration. Override the getItemOffsets and onDraw methods. For a sample implementation take a look here. This is a utility class with which you can add simple dividers through decoration. Instead, use ItemDecoration.

Also, I have written this library to simplify writing adapters with custom item decoration for RecyclerViews.

Sign in Get started. I'd ask him how his day went, and his response would be: William J Francis began programming computers at age eleven. Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intellige Specializing in embedded and mobile platforms, he has more than 20 years of professional software engineering under his belt, including a four year stint in the US Army's Military Intelligence Corps.

How to create simple view separators

Throughout his career William has published numerous technical articles, as well as the occasional short story. By William J. Francis in Software Engineer , in Developer on January 15, , 3: I'd define a simple XML drawable, like so: Editor's Picks. Inside the Raspberry Pi: How self-driving tractors, AI, and precision agriculture will save us from the impending food crisis. Smart farming: How IoT, robotics, and AI are tackling one of the biggest problems of the century.

Android app development for beginners - 25 - Android - Custom Line View between two points.

Agriculture 4. How digital farming is revolutionizing the future of food. The Brexit dilemma: Will London's start-ups stay or go? Can Russian hackers be stopped? Here's why it might take 20 years.