android:elevation doesn't work in L preview
Asked Answered
U

4

7

I've been playing with the L preview for a few days, and one thing that doesn't seem to work is the android:elevation attribute for a view. It works just fine when calling View.setElevation(), but it just seems to ignore the XML attribute. I'm using Android Studio 0.8.2 and have minSdkVersion and targetSdkVersion set to 'L', and compileSdkVersion set to 'android-L'. buildToolsVersion is "20.0.0". Here is a sample layout that doesn't work properly:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="10dp"
        android:elevation="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="top"/>

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="10dp"
        android:elevation="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bottom"/>

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

</LinearLayout>

Here is a screenshot of the result: imgur.com/mqeq9vK

According to the android:elevation I set, the bottom card should be flat on the "ground", but it isn't. In fact, it looks no different than the top card who's elevation is 5dp. Is this a known issue, and does it work for you?

EDIT: It seems this is only the case with CardView, or at least it isn't an issue with Buttons. In this layout I replaced CardView with Button, and even though the shadow is kind of screwed up (known bug), you can still see the difference in elevation.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="2dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="1dp"/>

</LinearLayout>

EDIT 2: It's pretty much confirmed that this issue only affects CardView, android:elevation works fine for any other view. For a detailed explanation of why this happens look at the accepted answer. In the meantime, my workaround is to replace the CardView with a FrameLayout and set android:background to a drawable. Here is an example of a card background drawable that works:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#fff" />
</shape>

I've also filed this as a bug, so please star this issue if it affects you. https://code.google.com/p/android-developer-preview/issues/detail?id=731

Unclasp answered 11/7, 2014 at 5:39 Comment(0)
G
8

CardView sets its own elevation during initialization, which will override whatever you set from XML. You should file this as a bug at https://code.google.com/p/android-developer-preview/wiki/FilingIssues?tm=3

@Override
public void initialize(CardViewDelegate cardView, Context context, int backgroundColor,
        float radius) {
    cardView.setBackgroundDrawable(new RoundRectDrawable(backgroundColor, radius));
    View view = (View) cardView;
    view.setClipToOutline(true);
    view.setElevation(context.getResources().getDimension(R.dimen.cardview_elevation));
}
Glochidium answered 11/7, 2014 at 19:49 Comment(3)
Thank you, this confirms what I suspected. I'll file that bug and in the mean time I'll work out a different method then.Unclasp
@Glochidium Do you know if this is still a bug now that Lollipop is officially out? I still can't get it to work in XML.Bacchanalia
You should use the cardElevation property (as mentioned in another answer from yigit, who is the developer for CardView).Glochidium
R
3

Please use cardElevation property to set elevation on the CardView. This will effect shadow size pre-L as well.

Also, see the documentation for other attributes.

Ruthie answered 22/10, 2014 at 18:22 Comment(1)
Seems like this was added after the official SDK released. Nonetheless, thanks for letting me know about this attribute! The ability to easily control shadow size on pre-lollipop is a definite benefit.Unclasp
I
1

Please add this in your root

xmlns:card_view="http://schemas.android.com/apk/res-auto"

below is the code for a cardview

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="10dp"
    android:id="@+id/view" >
    .
    .
    .
    .
    .
    Child
    .
    .
    .
    .

</android.support.v7.widget.CardView>
Indiscriminate answered 8/9, 2015 at 10:40 Comment(0)
W
0

Until the bug fix is released you can programmatically set it.

I've done it in a RecyclerView like this:

In your RecyclerView.Adapter:

public class MyAdapter extends RecyclerView.Adapter<TripsAdapter.ViewHolder> {

    /* ... */

    public class ViewHolder extends RecyclerView.ViewHolder {
        CardView card;

        public ViewHolder(View itemView) {
            card = (CardView) itemView.findViewById(R.id.card_view);
        }
    }

    /* ... */

    @Override public void onBindViewHolder(ViewHolder holder, int position) {
        /// ...

        // Update Card Elevation
        final float scale = mContext.getResources().getDisplayMetrics().density;
        holder.card.setElevation(1f * scale);    //TODO set this in xml once CardView elevation bug is fixed
    }

}
Woolf answered 21/10, 2014 at 3:27 Comment(1)
Not working also setElevation method is only available in android 5 so the OS version check is also needed to be added.Pr

© 2022 - 2024 — McMap. All rights reserved.