38

When do I use android:elevation and when app:elevation?

What's the difference between those two?

Amplify
  • 865
  • 1
  • 8
  • 18

3 Answers3

29

Hope I can help, Let's talk with an example:

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    ...
    android:elevation="@dimen/elevation_medium"
    /> 

The android:elevationattribute will work from the API level 21 and upper.

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    ...
    app:elevation="@dimen/elevation_medium"
    /> 

In this case the app:elevation attribute belongs to the FloatingActionButton styleable, inside de Android Design Support Library which will work from version 4 of the framework, instead version 21, the case of android:elevation.

saulmm
  • 2,398
  • 15
  • 21
  • 3
    The AppBarLayout has the app:elevation attribute but it still doesn't work. Seems like it disappeared in the recent support library updates. – Chris Aug 08 '16 at 16:32
19

app:elevation - if you are using support library (for supporting old versions of Android) android:elevation - if you are not using support library

Ilya Tretyakov
  • 6,848
  • 3
  • 28
  • 45
  • 4
    Note that this doesn't work with all ViewGroup. For example, I'm using support library, but I'am not able to use the app:elevation with the toolbar. – Filipe Brito Apr 25 '16 at 05:04
  • 4
    I can't use `app:elevation` with LinearLayout too. But `ViewCompat.setElevation(View view, float elevation);` works. – Weekend Feb 04 '17 at 07:54
2

There is one more thing:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/spacing"
    android:src="@drawable/svg_my_location"
    app:elevation="4dp" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/bottomSheet"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/shape_rect_round_white"
    app:elevation="8dp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>

With using app FloatingActionButton is visible

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/spacing"
    android:src="@drawable/svg_my_location"
    android:elevation="4dp" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/bottomSheet"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="@drawable/shape_rect_round_white"
    android:elevation="8dp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="100dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"/>

When you're using android FloatingActionButton is invisible, because it overlapped by bottom sheet

Andrii Turkovskyi
  • 27,554
  • 16
  • 95
  • 105