Android API – Dividers?
During day to day development you easily forget some neat little features that might have slipped through your mind. This time we are diving into a feature of the LinearLayout.
Imagine a simple horizontal LinearLayout, wherein 3 squares are shown:
The requirement is to show a divider between the squares, like so:
How would you tackle this?
You might create divider views inside the LinearLayout like below.
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <View /> <include layout="@layout/divider" /> <View /> <include layout="@layout/divider" /> <View /> </LinearLayout>
This will work for sure, but the LinearLayout possesses the same feature. Let me introduce you to android:divider
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:divider="@drawable/divider" android:dividerPadding="15dp" android:showDividers="beginning|middle|end"> <View /> <View /> <View /> </LinearLayout>
There are three settings you can play with.
android:showDividers
This can be beginning, end, middle or none.
By default this value is set to none, so remember to set a value. You can combine the values by adding a pipe.
android:divider
This tag decides how the divider is going to look like. Here you can add a color or drawable.
android:dividerPadding
Set the padding size used to inset dividers.
The video below will showcase multiple cases. You can find the code here.