17

The Android Design - Lists page describes "section dividers". I'm aware you can use addHeaderView() a ListView for a similar effect. I would like to use a "section divider" without a ListView, but rather a LinearLayout.

In the screenshot below, I'm referring to the blue text "Phone" and "Email" which also has an line below it. The screenshot is from Android Design - Text Fields

enter image description here

How do I go about adding it to my layout? It it simply a TextView plus a horizontal line?

gak
  • 29,596
  • 24
  • 111
  • 150

5 Answers5

46

I was looking for the same issue. I found an easy way to tell the app that a texview is a section separator:

<TextView
    android:id="@+id/address_label"
    style="?android:attr/listSeparatorTextViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Address"/>

The line:

style="?android:attr/listSeparatorTextViewStyle"

add the underline to the text and style it accordingli to the defaulf "Separator" theme.

Andrew Barber
  • 37,547
  • 20
  • 91
  • 118
MonkeyDroid
  • 637
  • 7
  • 14
4

Try to put this View after the TextView of "phone". In this view I have put in a background color that you can change to your desire. Best of luck.

<View
    android:layout_width="match_parent"
    android:layout_height="2dip"
    android:background="#FF909090" />
Jonathan Cast
  • 4,338
  • 16
  • 31
Aamirkhan
  • 6,853
  • 11
  • 45
  • 75
  • 2
    Just a quick remark.. With this solution you should use px instead of dip, because in smaller screens it can be converted to 0px and become invisible. – Thpramos Dec 15 '14 at 00:41
3

The solution ended up having an includable layout called util_horizontal_line_section.xml:

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

<View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:background="@color/sectionSeparatorColour"/>

With the sectionSeparatorColour entry in values/colors.xml:

<color name="sectionSeparatorColour">#a0a0a0</color>

Includable via:

<include layout="@layout/util_horizontal_line_section"/>
gak
  • 29,596
  • 24
  • 111
  • 150
2

On request of the asker of this question, I am writing my comment as an answer

Create a background image with a line at the bottom, and set it as background to your TextView.

Arif Nadeem
  • 8,136
  • 7
  • 42
  • 77
-1

TextView 2dp in height and width = match parent and set the background color as the color you want the line to be.

You can do vertical as well by reversing the two settings.

Barak
  • 16,117
  • 9
  • 49
  • 84