This example demonstrates how do I draw a line in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
There are many ways we can add a Horizontal line/rule in an Android Layout. xml file. One of the ways is to create a table row with background color and a height of say 1-2dp.
That line historically represents the margin of A4 paper, which is useful only if you need to print the code. Old school says that you should keep your code inside that margin, to guarantee much portability.
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" />
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
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 :)
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?
Try this
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="?android:attr/listDivider"/>
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#000000" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With