How to Add Shadow to the Fab Provided with the Android Support Design Library

How to add shadow to the FAB provided with the android support design library?

Simply setting app:borderWidth="0dp" resolve this issues for me.

Note: don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto" to your root layout.

This issue should be fixed in next release of android design library.

Fab in Lollipop shows strange shadow on long press

It turned out that the culprit was useCompatPadding.

Removing it solved the shadow problem.

To fix the margins I used this answer
which basically uses custom margins with two different styles, default and ond for api 21+

But this is in fact an ugly solution and the margins are not perfect, so I am open to better solutions.

How to remove FloatingActionButton's surrounding shadow?

Override the default elevation of the FAB by adding:

android:elevation="0dp"

Or in code call View.setElevation(float)

FAB - square on pre Lollipop and without shadow on Lollipop

Thanks to @harism comment, simply setting app:borderWidth="0dp" resolve both issues.

Note: don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto" to your root layout.

Elevation for FAB doesn't work

This is used for an ImageButon, not a FAbButton, but it simulates the Fab:
what you can do it is create a fab-btn.xml in drawable folder to simulate the shadow:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false">
<layer-list>
<item android:bottom="0dp" android:left="2dp" android:right="2dp" android:top="2dp">
<shape android:shape="oval">
<solid android:color="#44000000" />
</shape>
</item>
<item android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp">
<shape android:shape="oval">
<solid android:color="@color/orange" />
</shape>
</item>
</layer-list>
</item>
<item android:state_pressed="true">
<shape android:bottom="2dp" android:left="2dp" android:right="2dp" android:shape="oval" android:top="2dp">
<solid android:color="#E73700" />
</shape>
</item>

</selector>

In your main layout on the fab button:

 android:background="@drawable/fab_btn"
android:elevation="5dp"

If it is API 21+ it will take the levation, if not it will take the fab_btn.xml. It is up to you to update this file to have the desired shadow

Android design library FAB no shadow

Change xmlns:app="http://schemas.android.com/tools" to xmlns:app="http://schemas.android.com/apk/res-auto".



Related Topics



Leave a reply



Submit