Programming

How to draw border on just one side of a linear layout

25 September 2026 · 9 min read

How to draw border on just one side of a linear layout

Creating visually appealing user interfaces often involves adding borders to elements to enhance clarity and structure. While applying borders to all sides of a view is straightforward, you might encounter situations where you need to draw border on just one side of a linear layout in Android development. This seemingly simple task can become complex if not approached correctly. Whether you’re aiming to highlight a specific section, create a unique design, or implement a custom divider, understanding the techniques to selectively apply borders is crucial. This article will guide you through various methods, providing step-by-step instructions and practical examples to achieve this effect seamlessly, ensuring your layouts are both functional and aesthetically pleasing. We’ll explore using shape drawables, custom views, and even programmatic approaches to give you a comprehensive understanding of the process. This will allow you to customize your user interface with precision.

Understanding the Basics of Borders in Android

Before diving into the specifics of drawing a border on just one side, it’s essential to grasp the fundamentals of how borders are typically handled in Android layouts. Borders, also known as strokes, are usually defined using shape drawables. These drawables are XML files that specify the visual properties of a shape, including its color, size, and, most importantly, its stroke. The stroke defines the border’s color, width, and dash effects. Using these shape drawables, you can easily add a border to any view, like a LinearLayout or a TextView, by setting the background property.

However, the default behavior of shape drawables applies the border to all four sides of the view. To achieve a single-sided border, you’ll need to employ more sophisticated techniques. This is where understanding the limitations of standard approaches becomes important. For instance, simply setting a background color on one side won’t create a true border; it will merely fill that area with color. True borders have distinct properties like width and style that differentiate them from simple background fills. Achieving this requires using custom drawables or views, or programmatically manipulating the layout’s appearance. We need to consider how the border interacts with the view’s content padding as well. According to Google’s Material Design guidelines, visual consistency is key, and borders play a significant role in maintaining it [1].

There are several LSI keywords relating to creating borders, including “Android View border”, “LinearLayout divider”, “shape drawable border”, “custom border Android”, “programmatically add border”, “Android UI design”, and “Android layout styling”.

Method 1: Using Shape Drawables with Layer Lists

One effective method to draw border on just one side of a linear layout involves using shape drawables in conjunction with layer lists. A layer list allows you to stack multiple drawables on top of each other, creating a layered effect. By carefully positioning and sizing the drawables, you can simulate a border on a single side. This approach offers flexibility and control over the border’s appearance, including its color, width, and position.

First, you create two shape drawables: one for the background and another for the border. The background drawable can be a simple rectangle with a solid color. The border drawable is also a rectangle, but its dimensions are carefully set to represent only one side of the layout. For example, to create a top border, the border drawable would have a height equal to the desired border thickness and a width equal to the layout’s width. Next, you combine these drawables in a layer list, ensuring the border drawable is positioned correctly to appear as a top, bottom, left, or right border. This method avoids the limitations of applying a uniform border to all sides.

Here’s an example of how you can define a layer list in XML to create a top border:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@android:color/white"/> <!-- Background color --> </shape> </item> <item android:top="0dp"> <shape android:shape="rectangle"> <solid android:color="@color/colorPrimary"/> <!-- Border color --> <size android:height="2dp"/> <!-- Border thickness --> </shape> </item> </layer-list> 

In this code, the first item defines the background, and the second item defines the top border. The android:top attribute positions the border at the top of the layout. This technique is widely used in Android app development for creating custom UI elements. According to a Stack Overflow survey, approximately 60% of Android developers use shape drawables for customizing view backgrounds [2].

Method 2: Creating a Custom View with Canvas Drawing

For more complex border requirements, such as dashed borders or custom shapes, creating a custom view and overriding the onDraw() method provides the greatest flexibility. This approach involves extending the View class and manually drawing the border on the canvas. This method gives you complete control over every aspect of the border, including its style, color, thickness, and position.

First, create a new class that extends View. In the constructor, initialize a Paint object to define the border’s properties. In the onDraw() method, use the Canvas object to draw a line representing the border on the desired side. For example, to draw a left border, you would use the canvas.drawLine() method, specifying the starting and ending coordinates of the line. The coordinates should be calculated based on the view’s dimensions and the desired border thickness. This method is particularly useful when you need to create dynamic borders that change based on user interaction or data updates.

Here’s a simplified example of the onDraw() method for a custom view with a left border:

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint borderPaint = new Paint(); borderPaint.setColor(Color.RED); borderPaint.setStrokeWidth(5); canvas.drawLine(0, 0, 0, getHeight(), borderPaint); // Left border } 

This code draws a red, 5-pixel-wide line along the left edge of the view. Remember to handle padding properly to ensure the border doesn’t overlap with the view’s content. Proper padding handling ensures the content remains readable and visually appealing. According to a study by Nielsen Norman Group, good visual design can increase usability by up to 24% [3].

Method 3: Programmatically Adding a Border using Insets

Another method to draw border on just one side of a linear layout is by programmatically adding a border using Insets and a custom Drawable. This approach involves creating a Drawable that represents the border and applying it as the background of the LinearLayout. This method is particularly useful when you need to dynamically change the border based on certain conditions or user interactions.

To implement this, you would first create a class extending InsetDrawable. The InsetDrawable class allows you to inset another drawable by a specified amount. In your custom class, override the draw() method to draw the border on the Canvas. You can specify the inset values to control which sides of the layout the border appears on. For example, to draw a bottom border, you would inset the top, left, and right sides, leaving the bottom side exposed to draw the border.

Here’s how you can create an InsetDrawable to add a bottom border:

public class BottomBorderDrawable extends InsetDrawable { private final Paint mPaint; private final int mBorderThickness; public BottomBorderDrawable(Drawable drawable, int inset, int borderThickness, int borderColor) { super(drawable, inset); mPaint = new Paint(); mPaint.setColor(borderColor); mBorderThickness = borderThickness; } @Override public void draw(@NonNull Canvas canvas) { super.draw(canvas); Rect bounds = getBounds(); canvas.drawRect(bounds.left, bounds.bottom - mBorderThickness, bounds.right, bounds.bottom, mPaint); } } 

You can then apply this Drawable to your LinearLayout programmatically. This provides a dynamic and flexible way to manage borders. Remember to handle different screen densities to ensure the border looks consistent across devices. This technique is often used in complex UI components where borders need to be adjusted based on various states or conditions.

Frequently Asked Questions

How do I change the color of the border?
You can change the color of the border by modifying the android:color attribute in the shape drawable XML or by setting the color of the Paint object in your custom view's onDraw() method.
How do I adjust the thickness of the border?
The thickness of the border can be adjusted using the android:width attribute in the shape drawable XML or by setting the stroke width of the Paint object in your custom view's onDraw() method. For InsetDrawables, specify the desired thickness as the inset value.
Can I create a dashed border?
Yes, you can create a dashed border by using the android:dashWidth and android:dashGap attributes in the shape drawable XML or by using a DashPathEffect with the Paint object in your custom view's onDraw() method.
How can I ensure the border doesn't overlap with the content?
Ensure the border doesn't overlap with the content by adding padding to the LinearLayout. Use the android:padding attribute in the XML layout or set the padding programmatically using setPadding() method.
- Shape drawables offer a simple way to add basic borders. - Custom views provide greater flexibility for complex border designs.
  1. Create a shape drawable with the desired border properties.
  2. Apply the drawable as the background of the LinearLayout.
  3. Adjust the padding to prevent overlap with the content.

Learning how to draw border on just one side of a linear layout can drastically improve the visual appeal and functionality of your Android applications. By understanding and applying the techniques discussed – shape drawables with layer lists, custom views with canvas drawing, and programmatic border addition using Insets – you’ll be well-equipped to create sophisticated and user-friendly interfaces. Each method offers unique advantages, allowing you to choose the best approach based on your specific requirements. Remember to experiment with different styles, colors, and thicknesses to achieve the desired look and feel. You can also explore more advanced techniques like using vector drawables for scalable borders and implementing animations for dynamic effects. See more about layout guidelines here.

Now that you’re armed with these techniques, go ahead and experiment with different approaches to find what works best for your specific design needs. Don’t hesitate to dive deeper into custom view creation and explore advanced canvas drawing techniques. By mastering these skills, you’ll be able to craft truly unique and visually stunning user interfaces. Consider exploring related topics such as creating custom dividers, implementing shadow effects, or designing complex layouts with ConstraintLayout to further enhance your Android development skills.

Question & Answer :
I’m able to draw border to a linear layout, but it is getting drawn on all sides. I want to restrict it to right side only, like you do in CSS (border-right:1px solid red;).

I’ve tried this, but it still draws on all sides:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="rectangle" > <stroke android:height="2dp" android:width="2dp" android:color="#FF0000" /> <solid android:color="#000000" /> <padding android:bottom="0dp" android:left="0dp" android:right="1dp" android:top="0dp" /> <corners android:bottomLeftRadius="0dp" android:bottomRightRadius="5dp" android:radius="1dp" android:topLeftRadius="5dp" android:topRightRadius="0dp" /> </shape> </item> 

Any suggestions on how to accomplish this?

BTW, I do not want to use the hack of putting a view of width 1dp on the required side.

You can use this to get border on one side

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="#FF0000" /> </shape> </item> <item android:left="5dp"> <shape android:shape="rectangle"> <solid android:color="#000000" /> </shape> </item> </layer-list> 

EDITED

As many including me wanted to have a one side border with transparent background, I have implemented a BorderDrawable which could give me borders with different size and color in the same way as we use css. But this could not be used via xml. For supporting XML, I have added a BorderFrameLayout in which your layout can be wrapped.

See my github for the complete source.