This article demonstrates how to draw a horizontal and a vertical line in an Android app.
Although we have countless XML tags to meet almost all of our needs, unfortunately, we don’t have any tag such as <line> tag to draw the line. However, <view> tag can be used in XML as a workaround.
NOTE: The “view” tag is different than the View class in Android.
How to draw a Vertical line?
Consider the following XML code to draw a Vertical line in Android:
Here, we use the “View” tag to create a rectangle with a very small width such that it becomes a vertical line. In the above code, a red vertical line of height ‘560dp’ and width ‘2dp’ is drawn which is at a left margin of ‘175dp’ from the left side of the screen and at a top margin of ’20dp’ from the TextView that lies above it. The thickness of this line can be increased or decreased by increasing and decreasing the
layout_width of the “View” tag respectively.
Note: To run this code, we can copy and paste this code into the XML Visualizer provided by Udacity. Make sure to erase all the previously written code in the visualizer(if any).
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:
Here, a horizontal line was drawn by making a rectangle of very small height and the desired width such that it becomes a horizontal line. In the above code, a red 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 top margin of “270dp” from the TextView that lies above it. Here, the thickness of the line can be increased or decreased by increasing and decreasing the value of
layout_height in contrast to
layout_width for vertical lines.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- How to Publish Your Android App on Amazon App Store for Free?
- Android | Running your first Android app
- XML Parsing in Android using SAX Parser
- XML Parsing in Android using XmlPullParser
- XML Parsing in Android using DOM Parser
- How to Create Drawable Resource XML File in Android Studio?
- Android | Horizontal RecyclerView with Examples
- How to build a simple Calculator app using Android Studio?
- How to create a Facebook login using an Android App?
- How to create a Stopwatch App using Android Studio
- How to build a simple music player app using Android Studio
- How to create a Face Detection Android App using Machine Learning KIT on Firebase
- App Development for Android in 2017: Challenges and Solutions
- How to Create a COVID-19 Tracker Android App to See Details of any City and State in India?
- How to make a phone call from your Android App?
- Why Kotlin will replace Java for Android App Development
- Android | Creating a Calendar View app
- Adding Firebase to Android App
- How to change the color of Action Bar in an Android App?
- Android App Development Fundamentals for Beginners
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.