2

I'm trying to test FAB from android.support.design.widget.FloatingActionButton

When I set elevation from the xml layout, the shadow shows the elevation.

When I try to change elevation programmatically:

fab.setElevation(9);

I get an error saying that my API should be raised from 14 to 21.

What am I doing wrong?

Machavity
  • 30,841
  • 27
  • 92
  • 100
rommex
  • 763
  • 1
  • 8
  • 21
  • Elevation is only available on API 21+ – Chol Jul 08 '15 at 09:47
  • Chol, but why elevation works for API 14 through xml? That was the question. – rommex Jul 08 '15 at 10:57
  • Actually througt the xml it will be ignored if API<21 and there will be no shadow. In Eclipse, on the xml you can see a warning : Attribute "elevation" is only used in API level 21 and higher (current min is 11) – Chol Jul 08 '15 at 11:54

2 Answers2

2

For fab elevation use :

    fab.setCompatElevation(8f); //API>=21

This method :

    fab.setElevation(8f); // Works only for API >=21 for other layouts

Elevation only works for API>=21

abhishekm473
  • 99
  • 1
  • 2
  • 10
0

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

Chol
  • 2,097
  • 2
  • 16
  • 26