1

I'm trying to get a 4dp elevation on a button in a sample app and I can't seem to get any shadow to show up.

This is my code:

<LinearLayout 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"
          tools:context=".MainActivity"
          android:clipChildren="false"
          android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me"
        android:background="@drawable/myrect"
        android:elevation="4dp"/>
</LinearLayout>

With my background drawable being:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid android:color="#336699" />
    <corners android:radius="5dp" />
</shape>

My app.gradle is:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"
    ...
}

This is the result:

No drop shadow

I did see other posts with the same issue like this one: Elevation on Android Lollipop not working. They all point to a semi-transparent background as the issue, but as you can see here I'm using a full opacity one.

Any idea of what I'm missing?

Community
  • 1
  • 1
siger
  • 2,872
  • 1
  • 23
  • 37
  • Not really duplicate, but has the same answer as http://stackoverflow.com/questions/26473244/android-api-21-changes-my-custom-button-background, which is "set a stateListAnimator." – alanv Dec 19 '14 at 22:09

1 Answers1

0

For android version 5.0 & above try the Elevation for other views..

android:elevation="10dp"

For Buttons,

android:stateListAnimator="@anim/button_state_list_animator"

button_state_list_animator.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="@dimen/button_pressed_z_material"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType"/>
</set>
</item>
<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="0"
android:startDelay="@integer/button_pressed_animation_delay"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>
<item>
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="0"
android:valueTo="0"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="0"
android:valueType="floatType"/>
</set>
</item>
</selector>

below 5.0 version,

 android:background="@android:drawable/dialog_holo_light_frame"

My output:

enter image description here

Ranjith Kumar
  • 13,385
  • 9
  • 95
  • 126