Android how to make custom drawable shape

1.1k Views Asked by At

Hi I want to draw a custom shape in xml like below. How can I draw following shape in android?

How to draw a consultation fee shape in android using xml

square shape.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff"/>

    <stroke android:width="3dp"
        android:color="@color/white"
        />

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

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

There are 3 best solutions below

2
F.Mysir On BEST ANSWER

Create a new drawable xml named as svg:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="100dp"
    android:height="100dp"
    android:viewportWidth="100"
    android:viewportHeight="100">
    <path
        android:pathData="M78.2,90L50,61.8 21.8,90V10h56.4v40z"
        android:fillColor="@color/colorPrimary"/>
</vector>

change pathdata to alter the shape as you prefer...

Use it in your layout xml for example like:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/svg">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My text"
        android:layout_centerInParent="true"/>
</RelativeLayout>
0
Hassan On

You forgot this part android:shape="rectangle" in your root tag you can choose ring oval line and rectangle shape from it set your code like this

 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ffffffff"/>

<stroke android:width="3dp"
    android:color="@color/white"
    />

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

<corners android:radius="1dp"
    android:bottomRightRadius="1dp" android:bottomLeftRadius="1dp"
    android:topLeftRadius="1dp" android:topRightRadius="1dp"/>
</shape>
1
Pratyay Biswas On
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffffff" />

    <stroke android:width="3dp"
        android:color="@color/white" />

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

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

You forgot to give android:shape="rectangle" in shape tag. "line", "oval", "ring" can also be used for respective shape. If all the corners are 1dp just use android:radius="1dp".