0

I configured a button's elevation and translationZ in xml layout

Rendered shadow regardless of I applied in xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#aaa"
    android:orientation="horizontal"
    android:clipToPadding="false"
    tools:context="com.myapplication.MainActivity">

    <TextView
        android:id="@+id/ReportTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="report"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/TopLayout"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <LinearLayout
        android:id="@+id/TopLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@+id/ReportTextView"
        app:layout_constraintBottom_toTopOf="@+id/TopButton"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">

        <Button
            android:id="@+id/DecreaseZButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-Z"/>

        <Button
            android:id="@+id/DecreaseElevationButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="-E"/>

        <Button
            android:id="@+id/IncreaseElevationButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+E"/>

        <Button
            android:id="@+id/IncreaseZButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+Z"/>
    </LinearLayout>

    <!-- Main Button -->
    <Button
        android:id="@+id/TopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:elevation="40px"
        android:text="Top"
        android:translationZ="4px"
        android:layout_margin="50dp"
        app:layout_constraintBottom_toTopOf="@+id/BottomButton"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/TopLayout"/>**

    <Button
        android:id="@+id/BottomButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="1000dp"
        android:text="Bottom"
        app:layout_constraintTop_toBottomOf="@+id/TopButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>
</android.support.constraint.ConstraintLayout>

as code shown applied elevation is 40px and translationZ is 4px but when activity starts, elevation and translation will be reset (shadow will be shown regardless of what elevation and translationz are)

in code, I add 4 buttons for decreasing and increasing elevation and translationZ pragmatically. when I change elevation or translationZ property, shadow works correct

package com.myapplication;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.AsyncLayoutInflater;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private View TopButton;
    private View BottomButton;
    private View DecreaseZButton;
    private View DecreaseElevationButton;
    private View IncreaseElevationButton;
    private View IncreaseZButton;
    private TextView ReportTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ReportTextView = findViewById(R.id.ReportTextView);
        DecreaseZButton = findViewById(R.id.DecreaseZButton);
        DecreaseZButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("ElevationZ", String.format("DecreaseZButton. before: elevation: %s, translationZ: %s, z: %s, metrics: %s", TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
                TopButton.setTranslationZ(TopButton.getTranslationZ() - getApplicationContext().getResources().getDisplayMetrics().density);
                Log.i("ElevationZ", String.format("DecreaseZButton. after: elevation: %s, translationZ: %s, z: %s, metrics: %s", TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
                Reload("DecreaseZButton");
            }
        });
        DecreaseElevationButton = findViewById(R.id.DecreaseElevationButton);
        DecreaseElevationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("ElevationZ", String.format("DecreaseElevationButton. before: elevation: %s, translationZ: %s, z: %s, metrics: %s", TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
                TopButton.setElevation(TopButton.getElevation() - getApplicationContext().getResources().getDisplayMetrics().density);
                Log.i("ElevationZ", String.format("DecreaseElevationButton. after: elevation: %s, translationZ: %s, z: %s, metrics: %s", TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
                Reload("DecreaseElevationButton");
            }
        });
        IncreaseElevationButton = findViewById(R.id.IncreaseElevationButton);
        IncreaseElevationButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("ElevationZ", String.format("IncreaseElevationButton. before: elevation: %s, translationZ: %s, z: %s, metrics: %s", TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
                TopButton.setElevation(TopButton.getElevation() + getApplicationContext().getResources().getDisplayMetrics().density);
                Log.i("ElevationZ", String.format("IncreaseElevationButton. after: elevation: %s, translationZ: %s, z: %s, metrics: %s", TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
                Reload("IncreaseElevationButton");
            }
        });
        IncreaseZButton = findViewById(R.id.IncreaseZButton);
        IncreaseZButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("ElevationZ", String.format("IncreaseZButton. before: elevation: %s, translationZ: %s, z: %s, metrics: %s", TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
                TopButton.setTranslationZ(TopButton.getTranslationZ() + getApplicationContext().getResources().getDisplayMetrics().density);
                Log.i("ElevationZ", String.format("IncreaseZButton. after: elevation: %s, translationZ: %s, z: %s, metrics: %s", TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
                Reload("IncreaseZButton");
            }
        });

        TopButton = findViewById(R.id.TopButton);
        TopButton.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                TopButton.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                Reload("onGlobalLayout");
            }
        });
    }

    @Override
    protected void onResume() {
        Reload("onResume");
        super.onResume();
    }

    @Override
    protected void onStart() {
        Reload("onStart");
        super.onStart();
    }

    private void Reload(String when) {
        String report = "Z: %s, Elevation: %s, TranslateZ: %s";
        report = String.format(report, TopButton.getZ(), TopButton.getElevation(), TopButton.getTranslationZ());
        ReportTextView.setText(report);
        Log.i("ElevationZ", String.format("Reload. %s: elevation: %s, translationZ: %s, z: %s, metrics: %s", when, TopButton.getElevation(), TopButton.getTranslationZ(), TopButton.getZ(), getApplicationContext().getResources().getDisplayMetrics().density));
    }
}

In code I log everything, here is log output:

08-16 06:19:54.596 25372-25372/com.myapplication I/ElevationZ: Reload. onStart: elevation: 40.0, translationZ: 4.0, z: 44.0, metrics: 2.625
08-16 06:19:54.600 25372-25372/com.myapplication I/ElevationZ: Reload. onResume: elevation: 40.0, translationZ: 4.0, z: 44.0, metrics: 2.625
08-16 06:19:54.707 25372-25372/com.myapplication I/ElevationZ: Reload. onGlobalLayout: elevation: 5.25, translationZ: 4.0, z: 9.25, metrics: 2.625

here is my question: - why applied elevation and translationZ property will be reset by android?

Homayoun Behzadian
  • 1,053
  • 9
  • 26

0 Answers0