CardView elevation animation

I want to add elevation animation to my android.support.v7.widget.CardView , as well as the Button material style. I tried setting 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 an error:

The selector element must be declared.

What is the right way to do this?

+5
source share
2 answers

I tried my code, maybe you just added state to the second element of the selector.

So change this line

 <item> 

with this

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

Full 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> 
+1
source

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

You have to create on res/animator , if it does not exist, it's easy to create.

But if you are looking for a problem, it already gives you a possible solution enter image description here

0
source

Source: https://habr.com/ru/post/1261162/


All Articles