Elevation in Button is not showing the Shadow

1

Hello, I'm trying to use Elevation on the buttons on the following screen:

Butitisnotworking,Elevationisnotformingashadow.

FollowtheXML:

<?xmlversion="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"
tools:context="kiraitami.krtbeta03.MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/toobar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="23dp"
    android:background="#13000000"
    android:elevation="3dp"
    android:minHeight="?attr/actionBarSize"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/bg_mountain"
    android:layout_width="462dp"
    android:layout_height="777dp"
    android:scaleType="centerCrop"
    android:src="@drawable/mountain"
    app:layout_constraintBottom_toTopOf="parent"
    app:layout_constraintEnd_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent" />

<ImageView
    android:id="@+id/bg_diagonal"
    android:layout_width="match_parent"
    android:layout_height="485dp"
    android:scaleType="centerCrop"
    android:src="@drawable/diagonal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.231"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/bg_mountain"
    app:layout_constraintVertical_bias="0.818" />

<Button
    android:id="@+id/btn_damage"
    android:layout_width="311dp"
    android:layout_height="38dp"
    android:elevation="20dp"
    android:clipToPadding="false"
    android:layout_marginBottom="212dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/damagebtn"
    android:onClick="startDamageActivity"
    android:text="BOTAO 1"
    android:textAppearance="@style/TextAppearance.AppCompat"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.14"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/btn_audio"
    android:layout_width="311dp"
    android:layout_height="38dp"
    android:layout_marginBottom="80dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/audiobtn"
    android:elevation="10dp"
    android:text="BOTAO 3"
    android:textAppearance="@style/TextAppearance.AppCompat"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.14"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/btn_notes2"
    android:layout_width="311dp"
    android:layout_height="38dp"
    android:layout_marginBottom="144dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/notesbtn"
    android:elevation="8dp"
    android:text="BOTAO 2"
    android:textAppearance="@style/TextAppearance.AppCompat"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.14"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/button4"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@android:drawable/ic_lock_power_off"
    android:elevation="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

PS: The only place elevation is working is in the Toolbar

What should I do to fix this?

    
asked by anonymous 30.10.2017 / 15:22

1 answer

1

Assigning a background to the button will lose some of its characteristics. Among them are the elevation and the animation when clicking.

On the other hand, even without assigning a background you can not change the elevation just by assigning a value through android:elevation .

The elevation at the different states of a button (Enabled, Disabled, Focused, Pressed) is controlled by a StateListAnimator.

So to change elevation you will either have to build your own StateListAnimator or assign null to android:stateListAnimator .

android:stateListAnimator="@null"
android:elevation="20dp"

In your case I suggest that instead of assigning the image to backgroud use android:drawableLeft .

Anything like this:

<Button
    android:id="@+id/btn_notes2"
    android:layout_width="311dp"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.AppCompat"
    android:backgroundTint="#ffffff"
    android:drawableLeft="@drawable/notesbtn"
    android:text="BOTAO 2"/>

Note:Theeffectof elevation is not visible on Android devices with API less than 21.

    
30.10.2017 / 18:27