Android XML: android:elevation vs. app:elevation
Asked Answered
O

3

38

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

What's the difference between those two?

Overblown answered 10/10, 2015 at 13:5 Comment(1)
I googled, but did not find any useful results.Overblown
P
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.

Prefiguration answered 10/10, 2015 at 13:39 Comment(1)
The AppBarLayout has the app:elevation attribute but it still doesn't work. Seems like it disappeared in the recent support library updates.Hey
M
19

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

Mirthless answered 10/10, 2015 at 13:10 Comment(2)
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.Smackdab
I can't use app:elevation with LinearLayout too. But ViewCompat.setElevation(View view, float elevation); works.Planoconvex
C
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

Corneille answered 30/5, 2019 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.