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:
Now apply it as a background to your smaller layout:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/customborder">
That should do the trick.
Also see:
Creat XML called border.xml in drawable folder as below :
then add this to linear layout as backgound as this:
android:background="@drawable/border"
Create a one xml file in drawable folder
android:width="2dp"
android:color="#B40404" />
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
Now call this xml to your small layout background
android:background="@drawable/yourxml"
Try this:
For example, let's define res/drawable/my_custom_background.xml as:
(create this layout in your drawable folder) layout_border.xml
android:color="#FF0000" />
android:bottom="1dp" />
android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"
android:topRightRadius="0dp" />
main.xml
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/layout_border" />
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
android:shape="rectangle">
Then, in your LinearLayout View, add the border.xml as the background like this
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/border">
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);
I'll add Android docs link to other answers.
It describes all attributes of the Shape Drawable and stroke among them to set the border.
Example:
Red border with transparent background.
Don't want to create a drawable resource?
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:minHeight="128dp">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp"
android:background="@android:color/white">
It's not exactly an answer for the question but for them wich just want to display the border of elements for debugging you could just go in your devloppers settings and turn on the show layout bounds in the drawing section