Change the color of divider in LinearLayout
Asked Answered
G

3

12

May I know how I can change the color of divider in LinearLayout?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal"
    android:divider="?android:attr/dividerVertical"
    android:dividerPadding="12dip"
    android:showDividers="middle"
    android:background="#ff2d2d2d" >
...
</LinearLayout>

Do I need to manually copy 9 patch image from Android SDK into my project, and define my own attribute to refer it?

Gaffney answered 2/3, 2013 at 4:42 Comment(1)
Yes, You can create your own image(dividerVertical) and put it in drawable and can use it using android:divider="@drawable/dividerVertical". I have tried at my end and its workingSommersommers
S
28

It looks like android:divider attribute doesn't accept a color value. So you have to create a separate divider drawable in order to get it works:

divider.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="#f00" />

</shape>

layout.xml

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

Also, please note that android:divider is only available in android 3.0 or higher and it doesn't work in previous android versions.

Sheer answered 2/3, 2013 at 4:57 Comment(3)
You should get rid of the invalid answer.Antilles
how come i can't see the divider when using this xml ? i've even tried to use a different width for it and show the dividers on all cases...Luby
I think divider attrib no longer works. There is the View solution which does not depend on this attribute.Choose
V
-2

This is how I did it

      <ImageView
            android:id="@+id/imgVwmarkupborder"
            android:layout_width="280dp"
            android:layout_height="2dp"
            android:src="@android:color/white" />
Vagary answered 2/3, 2013 at 4:46 Comment(0)
F
-2
<View
    android:layout_width="fill_parent"
    android:layout_height="1dp"
    android:background="@android:color/white"/>

In my method i am use this one..

Fleetwood answered 2/3, 2013 at 4:55 Comment(1)
I know such method. I just wish to use the android:divider featureGaffney

© 2022 - 2024 — McMap. All rights reserved.