12

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:

  • Genymotion Google Nexus 5X - Marshmallow 6.0.0 - API 23
  • Genymotion Google Nexus 5 - Lollipop 5.1.0 - API 22
  • Genymotion Samsung Galaxy S2 - Jelly Bean 4.1.1 - API 16
  • Hardware device, Samsung Galaxy S5 mini - KitKat 4.4.2 - API 19

All same result.

I'm using:

  • Android Studio 2.1.2
  • minSdkVersion 16
  • targetSdkVersion 24

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.

Community
  • 1
  • 1
Ramiro
  • 788
  • 2
  • 8
  • 19
  • Directly add your button to ScrollView and see if its showing the shadow or not. It may be due to Linear and RelativeLayout which is wrapping . Check this link for reference http://stackoverflow.com/questions/35289356/card-view-not-proper-in-5-1-1-tablet-and-5-0-2-mobile-device – Sreehari Jul 09 '16 at 04:59
  • 1
    There is no such property `layout_topLeft` for any `View`..!! – Janki Gadhiya Jul 09 '16 at 04:59
  • 1
    @jankigadhiya yes You Are Right and also in Material design `android:elevation="2dp"` and `android:translationZ="2dp" ` is only work when you set `android:stateListAnimator="@null"` otherwise it is take deafult property. – Harshad Pansuriya Jul 09 '16 at 05:02
  • Yep, there was a mistake in the sample code, no layout_topLeft. Edited. Still checking out. Thanks! – Ramiro Jul 09 '16 at 05:44

2 Answers2

46

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

Tim
  • 38,263
  • 17
  • 115
  • 131
Harshad Pansuriya
  • 17,218
  • 7
  • 58
  • 86
  • Didn't work. Did you try yourself the stateListAnimator in a new Android Project? Thanks! – Ramiro Jul 09 '16 at 04:42
  • 1
    it works! But it works in any emulator with API level 21 or higher. It doesn't work with API 20 or lower. I will open up another question about an alternative for lower apis. Thanks! – Ramiro Jul 09 '16 at 07:03
0

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">
Khan
  • 3,350
  • 3
  • 32
  • 58