banner



How To Draw A Horizontal Line

How to depict Horizontal and Vertical lines in an Android App using XML

This article demonstrates how to draw a horizontal and a vertical line in an Android app.

Although we have countless XML tags to run into almost all of our needs, unfortunately, nosotros don't have any tag such as <line> tag to draw the line. However, <view> tag can be used in XML every bit a workaround.

Notation: The "view" tag is unlike than the View grade in Android.

How to draw a Vertical line?
Consider the post-obit XML code to describe a Vertical line in Android:

< LinearLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent"

android:orientation = "vertical" >

< TextView

android:layout_height = "wrap_content"

android:layout_width = "match_parent"

android:text = "Vertical Line"

android:background = "#512DA8"

android:textSize = "25sp"

android:gravity = "center_horizontal" />

< View

android:layout_width = "2dp"

android:layout_height = "560dp"

android:groundwork = "#E91E63"

android:layout_marginTop = "20dp"

android:layout_marginLeft = "175dp" />

</ LinearLayout >

Here, we use the "View" tag to create a rectangle with a very small-scale width such that it becomes a vertical line. In the higher up code, a blood-red vertical line of top '560dp' and width '2dp' is drawn which is at a left margin of '175dp' from the left side of the screen and at a pinnacle margin of '20dp' from the TextView that lies above information technology. The thickness of this line can be increased or decreased by increasing and decreasing the layout_width of the "View" tag respectively.

Annotation: To run this code, nosotros tin re-create and paste this code into the XML Visualizer provided by Udacity. Brand sure to erase all the previously written lawmaking in the visualizer(if any).

Output:

Drawing a Horizontal line:
A horizontal line can be drawn in a very similar way as a vertical line. Following is the XML code for a horizontal line:

< LinearLayout

android:layout_width = "match_parent"

android:layout_height = "match_parent"

android:orientation = "vertical" >

< TextView

android:layout_height = "wrap_content"

android:layout_width = "match_parent"

android:text = "Horizontal Line"

android:background = "#4CAF50"

android:textSize = "25sp"

android:gravity = "center_horizontal" />

< View

android:layout_width = "320dp"

android:layout_height = "2dp"

android:background = "#E91E63"

android:layout_marginTop = "270dp"

android:layout_marginLeft = "20dp" />

</ LinearLayout >

Here, a horizontal line was drawn by making a rectangle of very pocket-sized height and the desired width such that it becomes a horizontal line. In the above code, a cherry horizontal line of height "2dp" and width "320dp" is drawn which is at a left margin of "20dp" from the left side of the screen and at a pinnacle margin of "270dp" from the TextView that lies higher up it. Here, the thickness of the line can exist increased or decreased by increasing and decreasing the value of layout_height in dissimilarity to layout_width for vertical lines.

Output:


Source: https://www.geeksforgeeks.org/how-to-draw-horizontal-and-vertical-lines-in-an-android-app-using-xml/

Posted by: palmerbutcheall.blogspot.com

0 Response to "How To Draw A Horizontal Line"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel