How to add (vertical) divider to a horizontal LinearLayout?
Asked Answered
G

11

99

I'm trying to add a divider to a horizontal linear layout but am getting nowhere. The divider just doesn't show. I am a total newbie with Android.

This is my layout XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/llTopBar"
        android:orientation="horizontal"
        android:divider="#00ff00"
        android:dividerPadding="22dip"
        android:showDividers="middle">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="asdf" />
            
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="asdf" />
    
    </LinearLayout>
    
</RelativeLayout>
Goby answered 28/2, 2013 at 6:8 Comment(4)
What version of Android are you running this on? setDividerDrawable has only existed since API 11Julianjuliana
jelly bean 4.2 api 17Goby
If you've tried everything else, make sure the LinearLayout has the correct orientation. Setting a height for a divider with a horizontal orientation will be very confusing.Jermaine
dont forget the infuriating SHOWDIVIDERS item !!!!!!Contact
F
234

use this for horizontal divider

<View
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@color/honeycombish_blue" />

and this for vertical divider

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/honeycombish_blue" />

OR if you can use the LinearLayout divider, for horizontal divider

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:height="1dp"/>
    <solid android:color="#f6f6f6"/>
</shape>

and in LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@drawable/divider"
    android:orientation="vertical"
    android:showDividers="middle" >

If you want to user vertical divider then in place of android:height="1dp" in shape use android:width="1dp"

Tip: Don't forget the android:showDividers item.

Faraday answered 28/2, 2013 at 6:17 Comment(9)
thanks. but how will I add this to the "android:divider" attribute? basically, what I mean is, some kind automatic way to add the divider between each element? I mean isnt that why the android:divider attribute is there?Goby
@death_relic0 android:divider is avil for ListView ,Expandable Listview and TabWidgetBerzelius
thanks, but why is it here then :s developer.android.com/reference/android/widget/…Goby
It seems you should use any drawable, not colorCurricle
Seems you got your layout_width and layout_height values mixed up: for horizontal layout_width ought to be "fill_parent" and layout_height should be "1dp". Should be swapped similarly for the vertical divider.Unbolt
Keep in mind this only works on API 11 (Android 3.0 Honeycomb) and above.Oust
Why is this necessary? The docs state May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".Dremadremann
Is there any way to add vertical (top and bottom) padding to the divider. The dividerPadding options only adds left and right (start and end) padding.Shalne
Sir, it is oppositely mentioned..!!Whispering
O
70

Try this, create a divider in the res/drawable folder:

vertical_divider_1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">    
    <size android:width="1dip" />
    <solid android:color="#666666" />    
</shape> 

And use the divider attribute in LinearLayout like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal"
    android:divider="@drawable/vertical_divider_1"
    android:dividerPadding="12dip"
    android:showDividers="middle"
    android:background="#ffffff" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

Note: android:divider is only available in Android 3.0 (API level 11) or higher.

Orlando answered 28/2, 2013 at 6:24 Comment(2)
but that will just add one divider.. suppose I have like 10 elements, adding additional code for a divider between each element seems like a wasteGoby
@death_relic0 Why don't you create a separate layout for the divider and then use the include tag to add it anywhere and as many time you want. I think this would me better more and no waste.Orthodontia
S
41

It is easy to add divider to layout, we don't need a separate view.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:divider="?android:listDivider"
    android:dividerPadding="2.5dp"
    android:orientation="horizontal"
    android:showDividers="middle"
    android:weightSum="2" ></LinearLayout>

Above code make vertical divider for LinearLayout

State answered 21/10, 2014 at 6:51 Comment(4)
I always forget the showDividers attribute. Thank you!Calefactory
Thanks for the tip to use ?android:listDivider. I just noticed though that this is invisible in API 21 or higher. In lower API versions a small gray line is shownCandlefish
@KetanMehta we will define it with attribute 'android:divider', it's drawable or color.State
Does android:divider support API 15 + ?Snooty
F
17

Update: pre-Honeycomb using AppCompat

If you are using the AppCompat library v7 you may want to use the LinearLayoutCompat view. Using this approach you can use drawable dividers on Android 2.1, 2.2 and 2.3.

Example code:

<android.support.v7.widget.LinearLayoutCompat
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:showDividers="middle"
        app:divider="@drawable/divider">

drawable/divider.xml: (divider with some padding on the top and bottom)

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetBottom="2dp"
        android:insetTop="2dp">
    <shape>
        <size android:width="1dp" />
        <solid android:color="#FFCCCCCC" />
    </shape>
</inset>

Very important note: The LinearLayoutCompat view does not extend LinearLayout and therefor you should not use the android:showDividers or android:divider properties but the custom ones: app:showDividers and app:divider. In code you should also use the LinearLayoutCompat.LayoutParams not the LinearLayout.LayoutParams!

Fomentation answered 13/3, 2015 at 16:8 Comment(2)
Is this the only way to add vertical padding to a divider?Shalne
@Shalne no, you can always create a custom view or hack into existing view components. This is however the default and preferred method to do it.Homeopathy
P
8

I just ran into the same problem today. As the previous answers indicate, the problem stems from the use of a color in the divider tag, rather than a drawable. However, instead of writing my own drawable xml, I prefer to use themed attributes as much as possible. You can use the android:attr/dividerHorizontal and android:attr/dividerVertical to get a predefined drawable instead:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:showDividers="middle"
    android:divider="?android:attr/dividerVertical"
    android:orientation="horizontal">
    <!-- other views -->
</LinearLayout>

The attributes are available in API 11 and above.

Also, as mentioned by bocekm in his answer, the dividerPadding property does NOT add extra padding on either side of a vertical divider, as one might assume. Instead it defines top and bottom padding and thus may truncate the divider if it's too large.

Preliminaries answered 31/3, 2015 at 16:55 Comment(0)
E
7

You can use the built in divider, this will work for both orientations.

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:divider="?android:attr/listDivider"
  android:orientation="horizontal"
  android:showDividers="middle">
Explicit answered 29/2, 2016 at 16:53 Comment(0)
O
3

Frustratingly, you have to enable showing the dividers from code in your activity. For example:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the view to your layout
    setContentView(R.layout.yourlayout);

    // Find the LinearLayout within and enable the divider
    ((LinearLayout)v.findViewById(R.id.llTopBar)).
        setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);

}
Oversexed answered 28/6, 2013 at 22:4 Comment(1)
Although it's another way of doing it, there's no need.Swage
G
3

If the answer of Kapil Vats is not working try something like this:

drawable/divider_horizontal_green_22.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <size android:width="22dip"/>
    <solid android:color="#00ff00"/>

</shape>

layout/your_layout.xml

LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/llTopBar"
            android:orientation="horizontal"
            android:divider="@drawable/divider_horizontal_green_22"
            android:showDividers="middle"
           >

I encountered an issue where the padding attribute wasn't working, thus I had to set the height of the divider directly in the divider.

Note:

If you want to use it in vertical LinearLayout, make a new one, like this: drawable/divider_vertical_green_22.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <size android:height="22dip"/>
    <solid android:color="#00ff00"/>

</shape>
Grip answered 8/4, 2016 at 11:28 Comment(0)
E
2

Your divider may not be showing due to too large dividerPadding. You set 22dip, that means the divider is truncated by 22dip from top and by 22dip from bottom. If your layout height is less than or equal 44dip then no divider is visible.

Exile answered 15/5, 2014 at 16:9 Comment(0)
T
0

In order to get drawn, divider of LinearLayout must have some height while ColorDrawable (which is essentially #00ff00 as well as any other hardcoded color) doesn't have. Simple (and correct) way to solve this, is to wrap your color into some Drawable with predefined height, such as shape drawable

Trestlework answered 6/1, 2014 at 17:47 Comment(0)
A
-1

You have to create the any view for separater like textview or imageview then set the background for that if you have image else use the color as the background.

Hope this helps you.

Atop answered 28/2, 2013 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.