Android Appcompat 21 Elevation

Android AppCompat 21 Elevation

ViewCompat.setElevation(View, int) currently creates no shims.

The only way to simulate elevation right now is to apply a shadow pre-v21. Define your style/layout/drawable in values and override it in values-v21. For buttons I use style overrides. For layouts, I usually go for reference override (use @null to get rid of a drawable).

Hopefully in the future an update to the support library will add shims.

This reddit thread keeps track of said update.

Edit

The new support design library actually does create shims for the floating action button.

Appcompat v21 Toolbar elevation pre-lollipop

This worked for me very well:

<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="0dp">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary"
android:minHeight="?attr/actionBarSize" />

</android.support.v7.widget.CardView>

How to add elevation on android api 21

One work around is that you can put all these view inside CardView

Just put LinearLayout inside CardView

For example :-

<android.support.v7.widget.CardView
....
card_view:cardElevation="5dp"
card_view:cardCornerRadius="0dp">


<!-- other view that you want to elevate -->
<DrawerLayout ../> or <Toolbar ---->

</android.support.v7.widget.CardView>

android:elevation= doesn't work on devices pre-Lollipop with compile API21

Elevation requires the device to run Lollipop. See this answer on how to simulate elevation https://stackoverflow.com/a/26747592/680249

Setting Elevation in XML on AppCompat CardView on Android 5.0

It looks like a margin/padding problem, try to set the cardUseCompatPadding attribute to true. E.g.:

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="3dp">

Explanation from Android doc :

CardView adds additional padding to draw shadows on platforms before
L.

This may cause Cards to have different sizes between L and before L.
If you need to align CardView with other Views, you may need api
version specific dimension resources to account for the changes. As an
alternative, you can set cardUseCompatPadding flag to true and CardView will add the same padding values on platforms L and after.

Since setting cardUseCompatPadding flag to true adds unnecessary gaps in the UI, default value is false.

How to implement the Material-design Elevation for Pre-lollipop

You can mimic the elevation on pre-Lollipop with a official method.

I achieve same effect using,

  android:background="@android:drawable/dialog_holo_light_frame"

My tested output:

Sample Image

reference - https://stackoverflow.com/a/25683148/3879847

Thanks to user @Repo..

Update : If you want change color of this drawable try @Irfan answer below ↓

https://stackoverflow.com/a/40815944/3879847



Related Topics



Leave a reply



Submit