27

I am working on an android project and I have a LinearLayout which contains 2 horizontal buttons using borderless button style.

I am trying to show dividers in between each button but they are not showing up but I can't see any reason why not. Below is the XML layout:

<LinearLayout android:id="@+id/call_log_select_host_button_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:divider="#ffffff"
        android:showDividers="middle"
        android:dividerPadding="22dp">
        <Button android:id="@+id/call_log_select_btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            style="?android:attr/borderlessButtonStyle" />
        <Button android:id="@+id/call_log_select_btnBlock"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Block"
            style="?android:attr/borderlessButtonStyle" />
    </LinearLayout>

Thanks for any help you can provide.

Boardy
  • 35,417
  • 104
  • 256
  • 447

3 Answers3

97

Create a file mydivider.xml inside res/drawable and put the following shape

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

add the shape as divider for your layout

<LinearLayout android:id="@+id/call_log_select_host_button_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:divider="@drawable/mydivider"
        android:showDividers="middle"
        android:dividerPadding="22dp">
        <Button android:id="@+id/call_log_select_btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            style="?android:attr/borderlessButtonStyle" />
        <Button android:id="@+id/call_log_select_btnBlock"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Block"
            style="?android:attr/borderlessButtonStyle" />
    </LinearLayout>

or as workaround:

<LinearLayout android:id="@+id/call_log_select_host_button_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:divider="@drawable/mydivider"
        android:showDividers="middle"
        android:dividerPadding="22dp">
        <Button android:id="@+id/call_log_select_btnCancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel"
            style="?android:attr/borderlessButtonStyle" />

         <View    android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:background="#ffffff" />

        <Button android:id="@+id/call_log_select_btnBlock"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Block"
            style="?android:attr/borderlessButtonStyle" />
    </LinearLayout>
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
10

Try replacing android:divider="#ffffff" with android:divider="@android:color/white". My suspicion is that divider has to be a Drawable, and hard-coding the color may not treat it like a Drawable, but referencing it might.

Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • 1
    You're right. Switched from color to drawable and voila! Working. Accepted answer has great example – mente Oct 25 '13 at 15:14
0

You should add android:layout_width="fill_parent" and android:layout_height="fill_parent" in your LinearLayout. That way it will show ;)

Wannabe
  • 727
  • 1
  • 6
  • 21
  • Doing that breaks the layout as the content above the button group doesn't show only the buttons at the top of the activity and the dividers still didn't show – Boardy May 28 '13 at 22:18
  • and adding a `android:weightSum=""` in your LinearLayout? Will that solve your problem? – Wannabe May 28 '13 at 22:21
  • I've tried setting weightSum to 2 as explained in http://stackoverflow.com/questions/7452741/what-is-androidweightsum-in-android-and-how-does-it-work but it still shows the button group at the top taking the top with the content that's supposed to be above the buttons not showing and the dividers aren't showing – Boardy May 28 '13 at 22:26
  • `style="?android:attr/borderlessButtonStyle"` is only implemented in Android 4.0 if I'm correct. Do you have it set to android 4.0+? EDIT: it's implemented in API level 11 – Wannabe May 28 '13 at 22:32
  • I'm targetting Jelly Bean, the button borderless attribute is working as expected its just the dividers which aren't – Boardy May 28 '13 at 22:36