Search code examples
androidshapesandroid-drawabledrawrectangle

How to create rectangle with one shorter side?


I'm trying to draw a rectangle with one shorter side like in the image below:

enter image description here

This is what I have for the moment, but my code doesn't work all I'm drawing is a black rectangle.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="#000000" />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"/>
</shape>

Solution

  • You could rotate a rectangle. Just adjust the negative padding to your desired size.

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:top="-500dp">
            <rotate
                android:fromDegrees="-55"
                android:pivotX="100%"
                android:pivotY="100%">
                <shape android:shape="rectangle">
                    <solid android:color="#000" />
                </shape>
            </rotate>
        </item>
    </layer-list>