Skip to content Skip to sidebar Skip to footer

Android - How To Draw A Transparent Rectangle Bordered Shape With Only Its Corners Visible?

I want to draw a shape like this in android. I used the following code Copy

Solution 2:

<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solidandroid:color="@color/transparent" /><strokeandroid:width="1.5dp"android:color="@color/white" /><paddingandroid:bottom="5dp"android:left="5dp"android:right="5dp"android:top="5dp" /><cornersandroid:bottomLeftRadius="5dp"android:bottomRightRadius="5dp"android:topLeftRadius="5dp"android:topRightRadius="5dp" /></shape>

Solution 3:

<?xml version="1.0" encoding="utf-8"?><layer-listxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:bottom="1dp"android:left="-2dp"android:right="-2dp"android:top="-2dp"><shapeandroid:shape="rectangle" ><strokeandroid:width="1dp"android:color="#FF000000" /><solidandroid:color="#00FFFFFF" /><paddingandroid:left="10dp"android:right="10dp"android:top="10dp"android:bottom="10dp" /></shape></item>

Solution 4:

Below is the drawable that doesn't depends on the width and height of the rectangle, you can use this drawable for portrait and landscape :

<layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:width="8dp"android:height="48dp"android:gravity="top|left"><shapeandroid:shape="rectangle"><solidandroid:color="#000000" /></shape></item><itemandroid:width="48dp"android:height="8dp"android:gravity="top|left"><shapeandroid:shape="rectangle"><solidandroid:color="#000000" /></shape></item><itemandroid:width="8dp"android:height="48dp"android:gravity="top|right"><shapeandroid:shape="rectangle"><solidandroid:color="#000000" /></shape></item><itemandroid:width="48dp"android:height="8dp"android:gravity="top|right"><shapeandroid:shape="rectangle"><solidandroid:color="#000000" /></shape></item><itemandroid:width="8dp"android:height="48dp"android:gravity="bottom|left"><shapeandroid:shape="rectangle"><solidandroid:color="#000000" /></shape></item><itemandroid:width="48dp"android:height="8dp"android:gravity="bottom|left"><shapeandroid:shape="rectangle"><solidandroid:color="#000000" /></shape></item><itemandroid:width="8dp"android:height="48dp"android:gravity="bottom|right"><shapeandroid:shape="rectangle"><solidandroid:color="#000000" /></shape></item><itemandroid:width="48dp"android:height="8dp"android:gravity="bottom|right"><shapeandroid:shape="rectangle"><solidandroid:color="#000000" /></shape></item></layer-list>

Solution 5:

Here is what you want with little exception that skipped border have white color. I achieve it using layer list. you should try it if you need center transparent

enter image description here

<layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:width="300dp"android:height="300dp"><shapeandroid:shape="rectangle"><strokeandroid:width="15px"android:color="#000000" /><solidandroid:color="#00000000" /></shape></item><itemandroid:width="8dp"android:bottom="35dp"android:top="35dp"><shapeandroid:shape="rectangle"><solidandroid:color="#FFFFFF" /></shape></item><itemandroid:width="8dp"android:bottom="35dp"android:gravity="right"android:top="35dp"><shapeandroid:shape="rectangle"><solidandroid:color="#FFFFFF" /></shape></item><itemandroid:height="8dp"android:gravity="top"android:left="35dp"android:right="35dp"android:top="-1dp"><shapeandroid:shape="rectangle"><solidandroid:color="#FFFFFF" /></shape></item><itemandroid:height="8dp"android:gravity="bottom"android:left="35dp"android:right="35dp"><shapeandroid:shape="rectangle"><solidandroid:color="#FFFFFF" /></shape></item></layer-list>

Post a Comment for "Android - How To Draw A Transparent Rectangle Bordered Shape With Only Its Corners Visible?"