Dhiraj Devkar
Dhiraj Devkar

Reputation: 593

Custom shape in android using xml

I am trying to draw a custom shape which I can use as a background for my layout. But I am not able to do so. Is it possible to draw a shape as below using xml in android. I am not getting how to cut the semicircle shape from the vertical centres of rectangle.

enter image description here

Upvotes: 2

Views: 9800

Answers (7)

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Use layer-list to make this custom shape drawable.

/res/drawable/custom_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Transparent Rectangle -->
    <item>
        <shape android:shape="rectangle">
            <corners
                android:topLeftRadius="8dp"
                android:topRightRadius="8dp"
                android:bottomLeftRadius="4dp"
                android:bottomRightRadius="4dp" />

            <size
                android:width="300dp"
                android:height="100dp" />
            <solid android:color="#FFFFFF" />
        </shape>
    </item>

    <!-- Left Half-Circle -->
    <item
        android:left="-10dp"
        android:right="290dp"
        android:top="40dp"
        android:bottom="40dp">
        <shape android:shape="oval">
            <solid android:color="#000000" />
        </shape>
    </item>

    <!-- Right Half-Circle -->
    <item
        android:left="290dp"
        android:right="-10dp"
        android:top="40dp"
        android:bottom="40dp">
        <shape android:shape="oval">
            <solid android:color="#000000" />
        </shape>
    </item>

    <!-- Middle Dotted Line -->
    <item
        android:left="10dp"
        android:right="10dp">
        <shape android:shape="line">
            <stroke
                android:width="1dp"
                android:dashWidth="10px"
                android:dashGap="10px"
                android:color="#ff00"/>

        </shape>
    </item>

</layer-list>

USE:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_shape"/>

OUTPUT:

enter image description here

Hope this will help~

Upvotes: 6

Manikandan K
Manikandan K

Reputation: 921

Try below code

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >

        <solid android:color="#FFFFFF" />
        <corners android:radius="3dip" />
        <stroke
            android:width="1dp"
            android:color="#ED2A3C" /> 
    </shape> 

Upvotes: 0

Aditi
Aditi

Reputation: 455

Please refer the below xml, it may help you out

<?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="@android:color/black" />
        </shape>
    </item>
    <item
        android:bottom="10dp"
        android:left="10dp"
        android:right="10dp"
        android:top="10dp">
        <shape android:shape="rectangle">
            <size
                android:width="200dp"
                android:height="200dp" />
            <solid android:color="@color/colorAccent" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="line">
            <solid android:color="@android:color/black" />
            <stroke android:width="1dp"
                android:dashWidth="10px"
                android:dashGap="10px"/>
        </shape>
    </item>
    <item
        android:bottom="103dp"
        android:left="-10dp"
        android:right="205dp"
        android:top="103dp">
        <shape android:shape="oval">
            <size
                android:width="3dp"
                android:height="3dp" />
            <solid android:color="@android:color/black" />
            <stroke android:width="1dp" />
        </shape>
    </item>

    <item
        android:bottom="103dp"
        android:left="205dp"
        android:right="-10dp"
        android:top="103dp">
        <shape android:shape="oval">
            <size
                android:width="3dp"
                android:height="3dp" />
            <solid android:color="@android:color/black" />
            <stroke android:width="1dp" />
        </shape>
    </item>
</layer-list>

Upvotes: 0

Sushrita
Sushrita

Reputation: 725

Try this xml file in drawable :

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle">
<solid android:color="@color/primary" />
<corners android:radius="50dp"/>
</shape>

Upvotes: 0

Prashant Jajal
Prashant Jajal

Reputation: 3627

try bellow xml. save it in drawable.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="5dp"
android:bottomRightRadius="5dp"
/>
<solid
android:color="#ffffff"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>

<stroke
android:width="1dp"
android:color="#000000"
/>
</shape>

Upvotes: 1

Pruthviraj
Pruthviraj

Reputation: 578

It is better to use 9-Patch images. If you can create an image like above you can use https://romannurik.github.io/AndroidAssetStudio/nine-patches.html to create 9-patch image(define expandable area).

Upvotes: 0

pawegio
pawegio

Reputation: 1732

In your case it might be worth to create a resizable bitmap (9-Patch drawable). Follow this guide.

Upvotes: 1

Related Questions