Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elevation animation on click on CardView

I want to add the elevation animation to my android.support.v7.widget.CardView, just like the material style Buttons do. I've tried to set a StateListAnimator:

android:stateListAnimator="@anim/selector_raise"

which points to my selector in res/anim:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="true">
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ" android:valueTo="@dimen/touch_raise"
            android:valueType="floatType" />
    </item>
    <item>
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ" android:valueTo="0dp"
            android:valueType="floatType" />
    </item>
</selector>

but Android Studio gives me the error:

Element selector must be declared

What's the right way to do that?

like image 911
manfcas Avatar asked Dec 12 '16 22:12

manfcas


People also ask

How do you set elevation in CardView?

To change CardView's elevation in a backward compatible way, use setCardElevation . CardView will use elevation API on Lollipop and before Lollipop, it will change the shadow size. To avoid moving the View while shadow size is changing, shadow size is clamped by getMaxCardElevation .

How do I add ripple effect to CardView?

Adding Ripple Effect To enable this behavior, add the following attributes to your CardView . Using the android:foreground="? android:attr/selectableItemBackground" property on a CardView enables the ripple effect to originate from the touch origin.

How to add elevation to view in android?

To set the default (resting) elevation of a view, use the android:elevation attribute in the XML layout. To set the elevation of a view in the code of an activity, use the View. setElevation() method. To set the translation of a view, use the View.

What is android elevation?

Elevation (Android) Elevation is the relative depth, or distance, between two surfaces along the z-axis. Specifications: Elevation is measured in the same units as the x and y axes, typically in density-independent pixels (dp).


2 Answers

I've tried your code, maybe you have simply add the state to second selector element.

So change this line

<item>

with this

<item android:state_enabled="true" android:state_pressed="false">

The complete code will be

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="true">
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ" android:valueTo="@dimen/touch_raise"
            android:valueType="floatType" />
    </item>
    <item android:state_enabled="true" android:state_pressed="false">
        <objectAnimator android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ" android:valueTo="0dp"
            android:valueType="floatType" />
    </item>
</selector>
like image 198
Andrea Rosini Avatar answered Sep 28 '22 21:09

Andrea Rosini


You tried to create this .xml in res/anim folder.

You should create on res/animator, if don't exist is easy to create.

But if you search for the problem it already give you a possible solution enter image description here

like image 27
Canato Avatar answered Sep 28 '22 21:09

Canato