34

I'm trying to replace the third party FloatingActionButton with the native one which is packaged in library com.android.support:design:22.2.0.The default look has a dark shadow around the image,How can I get rid of it? I know the former one provides with the method setShadow(),but I just can't find similar one from the latter.

enter image description here

This is the related XML layout:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/alarm_front"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/btn_icon_alarm_notset" />

And I have set its background color to yellow.

mAlarmBtn.setBackgroundTintList(ColorStateList.valueOf(getResources().getColor(R.color.floatButtonColor)));
tounaobun
  • 14,042
  • 9
  • 49
  • 75

7 Answers7

53

Override the default elevation of the FAB by adding:

android:elevation="0dp"

Or in code call View.setElevation(float)

BrentM
  • 5,521
  • 3
  • 30
  • 38
  • 3
    setElevation is available at a minimum API level of 21,how can it work below LOLLIPOP? – tounaobun Jun 03 '15 at 08:10
  • You could simply stick with the old approach of creating your own ImageButton on pre-lollipop. As you did before they introduced FAB – Zsolt Boldizsár Jun 03 '15 at 08:17
  • Other developers are reporting issues with shadows on the new support FAB, although generally that the shadow is not showing which is the opposite problem, but maybe a related issue. http://stackoverflow.com/questions/30532863/how-to-add-shadow-to-the-fab-provided-with-the-android-support-design-library – BrentM Jun 03 '15 at 08:18
  • Then I would stick with the third party until there is a solution to the FAB of the design library.Thanks BrentM. – tounaobun Jun 03 '15 at 08:24
  • 21
    With the official FAB, you can use `app:elevation="0dp"` to do what you want. I tried on Android 4.1.1 and it works fine. I don't know if there is a Java solution yet. – Gaëtan Jul 01 '15 at 13:05
  • @Gaëtan, you should turn this to an answer, It really helped me. thanks – Tosin Onikute Nov 22 '15 at 03:18
  • 33
    use `app:elevation="0dp"` for compatibility – Leonardo Feb 16 '16 at 14:07
34

Add this

android:elevation="0dp" app:elevation="0dp"

It's will be like :

 <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add"
        android:elevation="0dp"
        app:elevation="0dp"
        app:fabSize="normal"
        android:scaleType="fitCenter"/>
26

Override the default elevation of the FAB by adding the following:

app:elevation="0dp"
Tomáš Hübelbauer
  • 6,810
  • 11
  • 59
  • 108
saurabh dhillon
  • 780
  • 7
  • 17
8

Make borderWidth to 0

app:borderWidth="0dp"
Rumit Patel
  • 4,807
  • 11
  • 42
  • 54
Sameer Khader
  • 157
  • 1
  • 5
5

If you are using the support libraries - the latest Android Studio templates us them. Check the imports

import android.support.design.widget.FloatingActionButton;
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//if using support app compat
fab.setCompatElevation(16.0f);

else if youre only supporting newer sdk versions

fab.setElevation();
//call requires SDK 21

see

.../app/build.gradle
  minSdkVersion 18    << less than 21 so req support libraries
  targetSdkVersion 25
brian.clear
  • 5,190
  • 2
  • 41
  • 60
4

Tried all suggestions above and nothing has worked for API 23 and higher. I've ended up with this, which has completely removed the shadow:

app:backgroundTint="@android:color/transparent"
app:borderWidth="0dp"

Below is how my button looks like now:

enter image description here

Before the change it looked as follows:

enter image description here

Oleg Gryb
  • 4,884
  • 1
  • 25
  • 39
0

This solutions works for ExtendedFloatingActionButton, too:

style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
StefanTo
  • 855
  • 1
  • 10
  • 27