Search code examples
androidxmlandroid-layoutandroid-linearlayout

How do I get a border of a Linear Layout without losing my current background for the layout


I was looking at solutions to achieve a border for a linear layout but most required setting the linear layout's background to a custom made XML in drawable. What is the best way to achieve a border without setting the background? Or is there a way to set the background for the layout in the custom XML that I am missing?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >



<LinearLayout
    android:id="@+id/bottom_panel"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:layout_alignParentBottom="true"
    android:background="#4C4E4D"
    android:baselineAligned="false"
    android:clickable="false"
    android:contextClickable="false">

    <Space
        android:layout_width="10dp"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </Space>

    <Button
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:id="@+id/homeIcon"
        android:layout_weight="10"
        android:background="@drawable/homeIcon"
        android:layout_margin="10dip" />

    <Space
        android:layout_width="15dip"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </Space>

    <Button
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:id="@+id/mag"
        android:background ="@drawable/blackMag"
        android:layout_weight="10"
        android:layout_margin="10dip" />

    <Space
        android:layout_width="15dp"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </Space>

    <Button
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:id="@+id/camera"
        android:background ="@drawable/blackCamera"
        android:layout_weight="10"
        android:layout_margin="10dip" />

    <Space
        android:layout_width="15dp"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </Space>

    <Button
        android:layout_width="0dip"
        android:layout_height="35dip"
        android:id="@+id/heart"
        android:layout_weight="10"
        android:background="@drawable/purpleHeart"
        android:layout_marginTop="10dip"
        android:layout_marginBottom="10dip"
        android:layout_margin="10dip" />

    <Space
        android:layout_width="15dp"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </Space>

    <Button
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:id="@+id/person"
        android:background="@drawable/BlackIcon"
        android:layout_weight="10"
        android:layout_margin="10dip" />

    <Space
        android:layout_width="10dp"
        android:layout_height="1dp"
        android:layout_weight="1" >
    </Space>


</LinearLayout>

</RelativeLayout>

Solution

  • Create a drawable xml file in res/drawable/my_custom_background.xml Also add the current background color in this fileand add the border and border color as well.

    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
        <!-- This is the stroke you want to define -->
        <stroke android:width="1dp"
            android:color="#cccccc"/>
    
        <!-- Optional, round your corners -->
        <corners
            android:bottomLeftRadius="0dp"
            android:topLeftRadius="0dp"
            android:bottomRightRadius="0dp"
            android:topRightRadius="0dp" />
    
        <!-- Optional, fill the rest of your background with a color or gradient, use transparent if you only want the border to be displayed-->
        <gradient
            android:startColor="#ffffff"
            android:endColor="#ffffff"
            android:angle="90"/>
    </shape>