Search code examples
androidxmldrawableshapes

how to create a semi oval shape (bending a line)


I'm trying to create a custom shape for my NavigationView footer, as background. but it's not so clean. This is what I have done:

enter image description here

And this is what I need:

enter image description here

Code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:gravity="bottom">
    <shape android:shape="rectangle">
        <solid android:color="@color/darkerGray" />
    </shape>
</item>
<item

    android:gravity="bottom|center_horizontal"
    android:top="50dp">
    <!--android:top="-40dp"-->

    <shape android:shape="oval">
        <solid android:color="#ffffffff" />
    </shape>
</item>
<item

    android:bottom="30dp"
    android:gravity="bottom">
    <shape android:shape="rectangle">
        <solid android:color="#ffffffff" />
    </shape>
</item>

Can you help me, please?


Solution

  • #. Update your custom drawable XML as below:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Container GRAY rectangle -->
        <item>
            <shape android:shape="rectangle">
    
                <size
                    android:width="250dp"
                    android:height="100dp" />
    
                <solid
                    android:color="@android:color/darker_gray" />
            </shape>
        </item>
    
        <!-- Top WHITE oval shape -->
        <item
            android:left="-25dp"
            android:right="-25dp"
            android:top="-50dp"
            android:bottom="50dp">
    
            <shape android:shape="oval">
    
                <solid
                    android:color="@android:color/white" />
            </shape>
        </item>
    </layer-list>
    

    PREVIEW:

    enter image description here

    #. Use this custom drawable in your layout as below:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:background="@android:color/black"
        android:padding="16dp">
    
        <!-- Custom Footer -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_shape">
    
        </LinearLayout>
    </LinearLayout>
    

    OUTPUT:

    enter image description here

    Hope this will help~