Hidden APIs of Android, Part 2— LinearLayout dividers

In this series I’m going to talk about APIs of Android, that most of developers, despite being quite experienced with Android SDK, do not know about their existence.

In part 1 we examined <blink> tag. In this episode we are going to dive into a feature of LinearLayout , that I have never seen anybody using.

Imagine a simple horizontal LinearLayout , wherein 5 fancy squares are shown:

Now the requirement is to introduce a divider between these 5 views like this:

How would you solve the problem?

Would you introduce a new divider view between each of these squares?

<LinearLayout ...>    <View .../> 
<include layout="@layout/divider"/>
<View .../>
<include layout="@layout/divider"/>
<View .../>
<include layout="@layout/divider"/>
<View .../>
<include layout="@layout/divider"/>
<View .../>
</LinearLayout>

Sure, this would solve the problem. But turns out that LinearLayout internally possesses such a feature. Please welcome android:divider tag:

<LinearLayout
...
android:divider="@drawable/divider"
android:showDividers="middle">

<View .../>
<View .../>
<View .../>
<View .../>
<View .../>
</LinearLayout>

Applying android:divider is not yet necessary - android:showDividers should also be set, otherwise it defaults to none . There are four options in total: middle , beginning , end and none .

As you have already guessed, beginning and end would apply divider only to beginning and and respectively.

U̶n̶f̶o̶r̶t̶u̶n̶a̶t̶e̶l̶y̶,̶ ̶t̶h̶e̶r̶e̶ ̶d̶o̶e̶s̶ ̶n̶o̶t̶ ̶e̶x̶i̶s̶t̶ ̶a̶ ̶w̶a̶y̶ ̶t̶o̶ ̶a̶p̶p̶l̶y̶ ̶d̶i̶v̶i̶d̶e̶r̶ ̶f̶o̶r̶ ̶b̶e̶g̶i̶n̶n̶i̶n̶g̶,̶ ̶m̶i̶d̶d̶l̶e̶ ̶a̶n̶d̶ ̶e̶n̶d̶ ̶s̶i̶m̶u̶l̶t̶a̶n̶e̶o̶u̶s̶l̶y̶,̶ ̶w̶h̶i̶c̶h̶ ̶i̶s̶ ̶a̶ ̶b̶i̶t̶ ̶w̶e̶i̶r̶d̶.̶ ̶

As pointed out by Aris Papadopoulos 👏, you can apply dividers for both beginning, middle and end just by bitwise OR-ing the flags: android:showDividers="beginning|middle|end".

Happy droiding! 🤖

Android enthusiast, Google Certified Android developer, Stackoverflower

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store