androidxmllayoutdividerbuttonbar

Divider issue in android xml layout


sample

My goal is to get the layout like the sample about. However, I can't get the small line above the button bar.

What I get is like this.

enter image description here

my xml code is :

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="18dp"
    android:layout_marginRight="18dp"
    android:layout_marginTop="24dp"
    >

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:layout_alignParentTop="true"
      android:layout_above="@+id/buttonBarLayout"
      android:showDividers="middle"
      android:divider="?android:dividerHorizontal">

    <TextView
        style="?android:listSeparatorTextViewStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Section header"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:showDividers="middle"
        android:divider="?android:dividerVertical"
        android:dividerPadding="8dp">

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textAppearance="?android:textAppearanceMedium"
          android:text="Sample item 1"
          android:layout_gravity="center_vertical"/>

      <ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_action_discard"
          style="?android:borderlessButtonStyle"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:baselineAligned="false"
        android:showDividers="middle"
        android:divider="?android:dividerVertical"
        android:dividerPadding="8dp">

      <TextView
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:textAppearance="?android:textAppearanceMedium"
          android:text="Sample item 2"
          android:layout_gravity="center_vertical"/>

      <ImageButton
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_action_discard"
          style="?android:borderlessButtonStyle"/>

    </LinearLayout>

  </LinearLayout>

  <LinearLayout
      android:id="@+id/buttonBarLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="horizontal"
      style="?android:buttonBarStyle"
      >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Discard"
        style="?android:buttonBarButtonStyle"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Save"
        style="?android:buttonBarButtonStyle"/>

  </LinearLayout>

</RelativeLayout>

I believe that the line is the middle divider. However, if I use LinearLayout the divider will show right under Sample item 2, which is not what I want.

So how can I get what the sample shows?


Solution

  • Add an extra View to your layout which will take no height as below...

    <View
        android:layout_width="match_parent"
        android:layout_height="0dip" />
    

    Then add the above View and the Linearlayout which holding Buttons inside another LinearLayout with android:divider="?android:dividerHorizontal" attribute as below...

    <LinearLayout
        android:id="@+id/buttonBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:divider="?android:dividerHorizontal"
        android:orientation="vertical"
        android:showDividers="middle" >
    
        <View
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:background="#000000" />
    
        <LinearLayout
            style="?android:buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="?android:dividerHorizontal"
            android:orientation="horizontal" >
    
            <Button
                style="?android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Discard" />
    
            <Button
                style="?android:buttonBarButtonStyle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Save" />
        </LinearLayout>
    
    </LinearLayout>
    

    Finally, your layout will be...

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:layout_marginTop="24dp" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/buttonBarLayout"
            android:layout_alignParentTop="true"
            android:divider="?android:dividerHorizontal"
            android:orientation="vertical"
            android:showDividers="middle" >
    
            <TextView
                style="?android:listSeparatorTextViewStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Section header" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                android:divider="?android:dividerVertical"
                android:dividerPadding="8dp"
                android:orientation="horizontal"
                android:showDividers="middle" >
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:text="Sample item 1"
                    android:textAppearance="?android:textAppearanceMedium" />
    
                <ImageButton
                    style="?android:borderlessButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                android:divider="?android:dividerVertical"
                android:dividerPadding="8dp"
                android:orientation="horizontal"
                android:showDividers="middle" >
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:text="Sample item 2"
                    android:textAppearance="?android:textAppearanceMedium" />
    
                <ImageButton
                    style="?android:borderlessButtonStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/buttonBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:divider="?android:dividerHorizontal"
            android:orientation="vertical"
            android:showDividers="middle" >
    
            <View
                android:layout_width="match_parent"
                android:layout_height="0dip" />
    
            <LinearLayout
                style="?android:buttonBarStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="?android:dividerHorizontal"
                android:orientation="horizontal" >
    
                <Button
                    style="?android:buttonBarButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Discard" />
    
                <Button
                    style="?android:buttonBarButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Save" />
            </LinearLayout>
        </LinearLayout>
    
    </RelativeLayout>
    

    Resulted Layout:

    enter image description here