Divider issue in android xml layout

3.3k Views Asked by At

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?

4

There are 4 best solutions below

0
Hamid Shatu On BEST ANSWER

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

0
Dasari On
<?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="Vertical"
      style="?android:buttonBarStyle"
      >
      <View android:id="@+id/divider"
        android:layout_widht="match_parent"
        android:layout_height = "1dp"
        android:background="@android:color/black"/>

  <LinearLayout
      android:id="@+id/buttonBarLayout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
       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>
 </LinearLayout>

</RelativeLayout>
0
kevz On

check this below layout. I've made few changes to add the horizontal divider above ButtonBarLayout.

<?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" >

<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"
  android:layout_marginLeft="18dp"
  android:layout_marginRight="18dp"
  android:layout_marginTop="24dp" >

<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"
      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"
      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="vertical"
  style="?android:buttonBarStyle">

  <View
      android:layout_width="fill_parent"
      android:layout_height="1dp"
      android:background="#F1F1F1" />

  <LinearLayout android:layout_width="match_parent"
  android:layout_height="wrap_content"
  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>

0
Alok Nath On

You have to give the id to the Linear layout above the button linear layout and place the view in between the button linear layout and linear layout above it.

<?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:id="@+id/layout1"
      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>


  <View
       android:id="@+id/view1"
android:layout_width="fill_parent"
 android:layout_height="1dp"
  android:layout_below="@+id/layout1"
 android:background="#000000" />

  <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>