0

I m trying to make a floating action button on pre lollipop devices. I have managed to make a decent button but it doesn't look good without any shadow. enter image description here

here is the circle.xml file

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

        <layer-list>

            <item>

                <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">

                    <solid android:color="#ffffffff" />

                    <size android:width="40dp" android:height="40dp" />
                </shape>
            </item>

        </layer-list>
    </item>
</selector>

and the imagebutton

 <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/circle"
        android:src="@drawable/ic_search"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:padding="10dp"
        android:layout_margin="16dp"
        />

How do I add shadow to it to make it look like material design floating action button ?

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126

1 Answers1

1

Use this as circle.xml instead.

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

<item android:top="8px">
    <layer-list>
        <item>
            <shape android:shape="oval">
                <solid android:color="#08000000"/>
                <padding
                    android:bottom="3px"
                    android:left="3px"
                    android:right="3px"
                    android:top="3px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#09000000"/>
                <padding
                    android:bottom="2px"
                    android:left="2px"
                    android:right="2px"
                    android:top="2px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#10000000"/>
                <padding
                    android:bottom="2px"
                    android:left="2px"
                    android:right="2px"
                    android:top="2px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#11000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#12000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#13000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#14000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>
        <item>
            <shape android:shape="oval">
                <solid android:color="#15000000"/>
                <padding
                    android:bottom="1px"
                    android:left="1px"
                    android:right="1px"
                    android:top="1px"
                    />
            </shape>
        </item>

    </layer-list>
</item>

<item>
    <shape android:shape="oval">
        <solid android:color="@color/ColorPrimary" />
    </shape>
</item>

</layer-list>

Source: http://www.android4devs.com/2015/03/how-to-make-floating-action-button-fab.html

Ahamed Abdul Rahman
  • 512
  • 1
  • 5
  • 18