Ramiro
Ramiro

Reputation: 968

Android elevation not showing shadow on Button

Can't get button shadows to show up.

Stripped down my code to the minimal example:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/main_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp">

            <Button
                android:id="@+id/my_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="20dp"
                android:elevation="10dp"
                android:translationZ="10dp"
                android:background="@android:color/holo_green_light"
                android:text="BUTTON"/>

        </RelativeLayout>

    </LinearLayout>

</ScrollView>

I need that layout structure.

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

}

Shadows is visible in Android Studio designer:

enter image description here

But not shown at runtime:

enter image description here

Tested on:

All same result.

I'm using:

Please create a new project in Andriod Studio, Empty Activity template, then copy and paste that code into activity_main.xml and MainActivity.java so you can test and tell me.

Upvotes: 21

Views: 45850

Answers (3)

elfefe
elfefe

Reputation: 78

You need to set android:outlineProvider="bounds" if elevation doesn't work with your view !

(I've simplified the code below for the exemple)

View:

<View
    android:id="@+id/view"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:outlineProvider="bounds"
    android:elevation="20dp" >

Result:

enter image description here

Upvotes: 1

User
User

Reputation: 4221

It was working in my case, Give it a try app:elevation

<Button
    android:id="@+id/my_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="20dp"
    android:layout_topLeft="20dp"
    app:elevation="2dp"
    android:translationZ="2dp"
    android:background="@android:color/holo_green_light"
    android:text="BUTTON">

Upvotes: -1

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20990

The default Button style under Material has a StateListAnimator that controls the android:elevation and android:translationZ properties.

copied from here

just add this property to your Button. you can set your own using the android:stateListAnimator property.

android:stateListAnimator="@null"

full code :

        <Button
                android:id="@+id/my_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="20dp"
                android:elevation="2dp"
                android:translationZ="2dp"
                android:stateListAnimator="@null"
                android:background="@android:color/holo_green_light"
                android:text="BUTTON">

UpDate :

for Understanding I set it 10dp..

xml code :

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp">

        <Button
            android:id="@+id/my_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="20dp"
            android:elevation="10dp"
            android:translationZ="10dp"
            android:stateListAnimator="@null"
            android:background="@android:color/holo_green_light"
            android:text="BUTTON"/>

    </RelativeLayout>

enter image description here

Upvotes: 67

Related Questions