How can I create a border around an Android LinearLayout?
Asked Answered
B

10

146

I have one big layout, and one smaller layout inside of it.

How do I create a line border around the small layout?

Brussels answered 27/2, 2013 at 11:50 Comment(1)
Have You tried #3496769 ?Stout
H
316

Sure. You can add a border to any layout you want. Basically, you need to create a custom drawable and add it as a background to your layout. example:

Create a file called customborder.xml in your drawable folder:

<?xml version="1.0" encoding="UTF-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <corners android:radius="20dp"/> 
   <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
   <stroke android:width="1dp" android:color="#CCCCCC"/>
 </shape>

Now apply it as a background to your smaller layout:

<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/customborder">

That should do the trick.

Also see:

Henryk answered 27/2, 2013 at 11:54 Comment(7)
<solid> will fill color in rectangle. u need to use <stroke> instead to draw border only.Postpaid
The trick here is actually the <padding>, took me a while to find why my shape did not work. With padding it works fine.Devries
Both <solid/> and <stroke/> fill the entire rectangle? Is this a bug in my code?Luckett
Add <stroke android:width="1dip" android:color="#CCCCCC" />, its works fine.Couperin
how to hide border on any one side alone ?Cavalry
Pierry 's comment makes this answer correct. Otherwise you can only get a full filled background.Galliard
@Galliard you are correct. I've edited it to reflect this. Thanks for the fix Pierry.Henryk
S
27

Creat XML called border.xml in drawable folder as below :

<?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" android:right="5dp"  android:top="5dp" >  
     <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
 </layer-list>

then add this to linear layout as backgound as this:

     android:background="@drawable/border"
Signify answered 27/2, 2013 at 11:56 Comment(0)
K
11

Try this:

For example, let's define res/drawable/my_custom_background.xml as:

(create this layout in your drawable folder) layout_border.xml

  <?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:width="2dp" android:height="2dp"
                android:color="#FF0000" />
            <solid android:color="#000000" />
            <padding android:left="1dp" android:top="1dp" android:right="1dp"
                android:bottom="1dp" />

            <corners android:radius="1dp" android:bottomRightRadius="5dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"
                android:topRightRadius="0dp" />
        </shape>
    </item>

</layer-list>

main.xml

<LinearLayout 
    android:layout_gravity="center"
    android:layout_width="200dp" 
    android:layout_height="200dp"   
    android:background="@drawable/layout_border" />
</LinearLayout>
Knockwurst answered 27/2, 2013 at 12:0 Comment(0)
D
11

Create a one xml file in drawable folder

<stroke
    android:width="2dp"
    android:color="#B40404" />

<padding
    android:bottom="5dp"
    android:left="5dp"
    android:right="5dp"
    android:top="5dp" />

<corners android:radius="4dp" />

Now call this xml to your small layout background

android:background="@drawable/yourxml"

Diphyllous answered 28/2, 2013 at 6:38 Comment(1)
Element stroke is not allowed here.Protraction
R
5

This solution will only add the border, the body of the LinearLayout will be transparent.

First, Create this border drawable in the drawable folder, border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="2dp" android:color="#ec0606"/>
    <corners android:radius="10dp"/>
</shape>

Then, in your LinearLayout View, add the border.xml as the background like this

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/border">
Rah answered 24/3, 2016 at 15:3 Comment(0)
A
4

you can do it Pragmatically also

  GradientDrawable gradientDrawable=new GradientDrawable();
   gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input));

Then set the background of layout as :

LinearLayout layout = (LinearLayout ) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);
Arellano answered 28/9, 2016 at 11:28 Comment(0)
M
4

I'll add Android docs link to other answers.

https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

It describes all attributes of the Shape Drawable and stroke among them to set the border.

Example:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <stroke android:width="1dp" android:color="#F00"/>
  <solid android:color="#0000"/>
</shape>

Red border with transparent background.

Materfamilias answered 14/1, 2018 at 14:33 Comment(1)
The solid part allows setting tint to border only. +1Calix
I
1

Don't want to create a drawable resource?

  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:minHeight="128dp">

    <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="1dp"
      android:background="@android:color/white">

      <TextView ... />
    </FrameLayout>
  </FrameLayout>
Infraction answered 16/5, 2019 at 22:28 Comment(0)
P
0

Try This in your res/drawable

    <?xml version="1.0" encoding="UTF-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
  <shape
    android:shape="rectangle">
    <padding android:left="15dp"
        android:right="15dp"
        android:top="15dp"
        android:bottom="15dp"/>
    <stroke android:width="10dp"
        android:color="@color/colorPrimary"/>
  </shape>
</item><item android:left="-5dp"
        android:right="-5dp"
        android:top="-5dp"
        android:bottom="-5dp">
  <shape android:shape="rectangle">
    <solid android:color="@color/background" />
  </shape></item></layer-list>
Philippopolis answered 11/8, 2020 at 5:30 Comment(0)
B
0

Nobody seems to be using the method we came up with so I thought I would share, as it involves much simpler code. Simply nest one layout inside another with padding, and the outer one can be colored, making a frame.

        <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/llSignatureBorder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@android:color/black"
        android:orientation="vertical"
        android:padding="4dp">

        <com.yourmom.mobiledriverapp.Controls.SignatureArea
            android:id="@+id/signatureArea"
            android:layout_width="@dimen/signaturearea_width"
            android:layout_height="@dimen/signaturearea_height"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp" />

    </androidx.appcompat.widget.LinearLayoutCompat>
Banlieue answered 19/9, 2022 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.