Android draw a Horizontal line between views

AndroidAndroid LayoutListviewAndroid Intent

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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: https://stackoverflow.com/questions/5049852/android-drawing-separator-divider-line-in-layout

Solution 5 - Android

Try this

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="?android:attr/listDivider"/>

Solution 6 - Android

After many years, finally there is a proper view to display separators/dividers/horizontal lines on XML layouts. The team who develops the Material Components for Android has released the version 1.5.0-alpha01 which adds the MaterialDivider class. This allows to add dividers to layouts as said previously.

First, in your build.gradle add the following:

implementation 'com.google.android.material:material:1.5.0-alpha01'

Then, on a XML layout you can write:

<com.google.android.material.divider.MaterialDivider
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

And voila you have a divider. If you want to customize the color of the divider, there is an xml attribute: app:dividerColor. This view also allows theming via styles. For more info you can check this link.

Solution 7 - Android

<View
       android:id="@+id/view"
       android:layout_width="match_parent"
       android:layout_height="2dp"
       android:background="#000000" />

Solution 8 - Android

An improvement of the accepted answer by Bhoomika for a more polished look:

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

android:alpha is used to set the opacity of views.

Solution 9 - Android

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

Solution 10 - Android

Try this works for me

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

Solution 11 - Android

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

Solution 12 - Android

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.

Solution 13 - Android

Try this

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

Solution 14 - Android

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.

Solution 15 - Android

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.

Solution 16 - Android

----> Simple one

 <TextView
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#c0c0c0"
    android:id="@+id/your_id"
    android:layout_marginTop="160dp" />

Solution 17 - Android

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>

Solution 18 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStringView Question on Stackoverflow
Solution 1 - AndroidBhoomika BrahmbhattView Answer on Stackoverflow
Solution 2 - AndroidvovahostView Answer on Stackoverflow
Solution 3 - AndroidzozelfelfoView Answer on Stackoverflow
Solution 4 - AndroidonexfView Answer on Stackoverflow
Solution 5 - AndroidXar-e-ahmer KhanView Answer on Stackoverflow
Solution 6 - AndroidXamView Answer on Stackoverflow
Solution 7 - AndroidJitesh DalsaniyaView Answer on Stackoverflow
Solution 8 - AndroidRonnieView Answer on Stackoverflow
Solution 9 - AndroidKaran DatwaniView Answer on Stackoverflow
Solution 10 - AndroidHiren PatelView Answer on Stackoverflow
Solution 11 - AndroidRvSingh3213View Answer on Stackoverflow
Solution 12 - AndroidRavi BhattView Answer on Stackoverflow
Solution 13 - AndroidMostafa HassanView Answer on Stackoverflow
Solution 14 - Android18446744073709551615View Answer on Stackoverflow
Solution 15 - AndroiddeepankarView Answer on Stackoverflow
Solution 16 - Androidshreedhar bhatView Answer on Stackoverflow
Solution 17 - AndroidSamView Answer on Stackoverflow
Solution 18 - AndroidAbdul WajidView Answer on Stackoverflow