111

I have My Layout like below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<TextView
        android:id="@+id/textView1"
        style="@style/behindMenuItemLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Twitter Feeds"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/list"
        android:layout_width="350dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/textView1"
        style="@style/behindMenuItemLabel1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:text="FaceBook Feeds" />

    <ListView
        android:id="@+id/list1"
        android:layout_width="350dp"
        android:layout_height="50dp" />

</LinearLayout>

My Requirement is to draw a Horizontal line between TextView and ListView

Could anyone help?

Charuක
  • 12,229
  • 5
  • 43
  • 83
String
  • 3,462
  • 9
  • 39
  • 63
  • You can add line to background of existing view's and not add extra one. Or you can put all your views inside listView. listView can draw separators. simplest ways is to add extra view. – Leonidos Oct 01 '13 at 11:59

16 Answers16

303

It will draw Silver gray colored Line between TextView & ListView

<TextView
    android:id="@+id/textView1"
    style="@style/behindMenuItemLabel1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:text="FaceBook Feeds" />

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#c0c0c0"/>

<ListView
    android:id="@+id/list1"
    android:layout_width="350dp"
    android:layout_height="50dp" />
capo11
  • 634
  • 3
  • 10
  • 25
Bhoomika Brahmbhatt
  • 7,032
  • 3
  • 25
  • 43
  • 13
    This hardcoded color is like using inline css in a webpage. Why is this voted up so much? It should use android:background="?android:attr/dividerHorizontal" for example. – Roel Feb 01 '16 at 15:03
  • 6
    Because it's easy to implement – Denny Oct 10 '18 at 16:24
  • 3
    @Roel It is simple, easy and it works. And it s straightforward to reference a colour in stead of hardcoding it. I tried your suggestion, which I think would be technically better, but it didn't seem to work - I didn't get any dividers in my layouts. How should I use it? Thank you. – nsandersen Jul 04 '19 at 22:39
  • 1
    @Roel What is the downside of using it? – nibbana Dec 08 '20 at 09:42
  • At least set the colors in a seperate file and use color variables. The downside is that you may have to change many files if you want to change the color / styling of your app. – Roel Dec 09 '20 at 12:31
24

You should use the new lightweight View Space to draw dividers. Your layout will load faster if you will use Space instead of View.

Horizontal divider:

<android.support.v4.widget.Space
        android:layout_height="1dp"
        android:layout_width="match_parent" /> 

Vertical divider:

<android.support.v4.widget.Space
        android:layout_height="match_parent"
        android:layout_width="1dp" />

You can also add a background:

<android.support.v4.widget.Space
        android:layout_height="match_parent"
        android:layout_width="1dp"
        android:background="?android:attr/listDivider"/>

Usage example:

....
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="One"/>

<android.support.v4.widget.Space
    android:layout_height="match_parent"
    android:layout_width="1dp"
    android:background="?android:attr/listDivider"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Two"/>

<android.support.v4.widget.Space
    android:layout_height="match_parent"
    android:layout_width="1dp"
    android:background="?android:attr/listDivider"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Three"/>
....

In order to use Space you should add the dependency in your build.gradle:

dependencies {
    compile 'com.android.support:support-v4:22.1.+'
}

Documentation https://developer.android.com/reference/android/support/v4/widget/Space.html

vovahost
  • 25,072
  • 12
  • 95
  • 99
  • 14
    The use of `Space` is going to be disappointing because `Space` draws nothing (not even a background). It's sole job is to take up space on the screen. The solution is to use a vanilla `View` element instead. Then the background will be drawn as desired. (You can also then avoid the support library dependency if it's not otherwise needed.) – Ted Hopp Sep 10 '15 at 01:21
  • 7
    `Space` inherits from `View` and therefore it inherits all the attributes defined by `View`. (Similarly, `LinearLayout` inherits the `textAlignment` attribute even though it doesn't itself display any text.) Feel free to test this for yourself (or just look at the source code) and you will find that a `Space` ignores all drawing-related attributes. – Ted Hopp Sep 11 '15 at 16:30
  • 1
    Yes, `Space` doesn't draw a background, it's transparent. – vovahost Jul 17 '19 at 10:24
21

add something like this in your layout between the views you want to separate:

  <View
       android:id="@+id/SplitLine_hor1"
       android:layout_width="match_parent"
       android:layout_height= "2dp"
       android:background="@color/gray" />

Hope it helps :)

zozelfelfo
  • 3,648
  • 2
  • 18
  • 34
17

Creating it once and using it wherever needed is a good idea. Add this in your styles.xml:

<style name="Divider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:background">?android:attr/listDivider</item>
</style>

and add this in your xml code, where a line divider is needed:

<View style="@style/Divider"/>

Originally answered by toddles_fp to this question: Android Drawing Separator/Divider Line in Layout?

onexf
  • 3,244
  • 3
  • 20
  • 34
  • wow. This is the best. wish you could also add example to demontsrtae use of vertical and horizontal divders for completeness. – nyxee Oct 20 '20 at 13:21
10

Try this

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="?android:attr/listDivider"/>
Xar-e-ahmer Khan
  • 1,424
  • 15
  • 25
6
<View
       android:id="@+id/view"
       android:layout_width="match_parent"
       android:layout_height="2dp"
       android:background="#000000" />
Jitesh Dalsaniya
  • 1,837
  • 2
  • 19
  • 35
2

You can put this view between your views to imitate the line

<View
  android:layout_width="fill_parent"
  android:layout_height="2dp"
  android:background="#c0c0c0"/>
Lamorak
  • 10,190
  • 8
  • 44
  • 55
Karan Datwani
  • 410
  • 6
  • 6
2

Try this works for me

 <View android:layout_width="1dp"
       android:layout_height="match_parent"
       android:background="@color/tw_composer" />
ʍѳђઽ૯ท
  • 15,369
  • 7
  • 47
  • 103
1

In each parent LinearLayout for which you want dividers between components, add android:divider="?android:dividerHorizontal" or android:divider="?android:dividerVertical.

Choose appropriate between them as per orientation of your LinearLayout.

Till I know, this resource style is added from Android 4.3.

Ravi Bhatt
  • 1,832
  • 1
  • 14
  • 27
1

Try this

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>
Damian Kozlak
  • 6,719
  • 10
  • 42
  • 50
1

----> Simple one

 <TextView
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#c0c0c0"
    android:id="@+id/your_id"
    android:layout_marginTop="160dp" />
shreedhar bhat
  • 4,223
  • 1
  • 14
  • 23
1

Create Horizontal line

If your are using TextView and then you want to put a Line then use View this way and you can use any color like Blue, Red or black mentioning background color.

 <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@android:color/black"></view>

enter image description here

Ajji
  • 2,721
  • 2
  • 26
  • 28
0

A View whose background color you can specify would do (height=a few dpi). Looked in real code and here it is:

        <LinearLayout
            android:id="@+id/lineA"
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:background="#000000" />

Note that it may be any kind of View.

18446744073709551615
  • 14,600
  • 3
  • 82
  • 116
0

If you does not want to use an extra view just for underlines. Add this style on your textView.

style="?android:listSeparatorTextViewStyle"

Just down side is it will add extra properties like

android:textStyle="bold"
android:textAllCaps="true"

which you can easily override.

Charuක
  • 12,229
  • 5
  • 43
  • 83
deepankar
  • 341
  • 5
  • 12
0

which work for me is

    <view
    android:layout_below="@+id/kaimla_number"
    android:layout_width="match_parent"
    android:layout_height="3dp"
    android:background="@color/golden"></view>
0

 <view
        android:id="@+id/blackLine"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

app:layout_constraintStart_toStartOf="parent"/>