Hi I am developing android application in which I am trying to implement new material design features. I tried to apply both elevation property and TranslationZ property but it is not working.
<Button
android:id="@+id/button1"
style="@style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Name" />
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#0073ff" />
<corners android:radius="16dp" />
</shape>
<style name="ButtonStyle">
<item name="android:elevation">8dp</item>
<item name="android:translationZ">8dp</item>
<item name="android:background">@drawable/file</item>
</style>
TranslationZ is a dynamic property used for animation. Basically it's needed to nicely handle elevation changes. When you press a button, its elevation remains unchanged and its translationZ is being animated. This way the View always knows what was the original elevation value and can respond correctly to multiple touch events.
Internally Z = elevation + translationZ
Simple Fix
Your problem with losing the touch feedback & Z animations when updating the style comes from not extending the parent android:Widget.Material.Button in your style. If you do that, you won't have to do any manual animation or touch feedback. Just overwrite what you need!
Z-depth explanation
As per the documentation,
TranslationZ
is just the dynamic component of the Z value. What this means is that when you animate your views in the Z-axis (withViewPropertyAnimator
and such), the animation state will start from the static component of the Z value, theelevation
, and will end at the full Z value with with thetranslation
component being the change in between, or the delta.Z = elevation + translationZ
To implement an animation with translationZ, you'd need to make a
StateListAnimator
. In your particular case, because you are not animating anything, in order to set a Z-depth to yourButton
, you just needelevation
.