6

I am trying to make a button that has a shadow using elevation with a background image being my sign in with gmail png. The button is contained within a relative layout. The elevation won't show no matter what I try to do. I tried solutions from other forum questions and none worked.

Here is my code for the button

<Button
    android:id="@+id/google"
    android:layout_width="270dp"
    android:layout_height="38dp"
    android:layout_marginTop="30dp"
    android:layout_centerHorizontal="true"
    android:background="@drawable/google"
    android:elevation="10dp"
    android:layout_below="@id/slogan"/>

The google drawable is a png image that I exported from Adobe XD. Could someone please give me a pointer on what I am doing wrong? Thanks.

Additionally, I realize that if I set the background of the button to android:color/white the shadow appears. So I think the issue is with the png drawable? Does elevation not work with png images? Is there a workaround?

Jeffrey Chou
  • 331
  • 2
  • 4
  • 15

5 Answers5

10

Use below the line of code to show an elevation in button

android:outlineProvider="bounds" – K. Sopheak

That works, thanks!

BlackBlind
  • 772
  • 9
  • 26
Jeffrey Chou
  • 331
  • 2
  • 4
  • 15
  • 4
    For me this one resulted in strange background behaviour but android:outlineProvider="paddedBounds" was the key. – Shyri Apr 09 '18 at 12:21
  • 1
    for me it works only with: elevation, translationZ, outlineProvider=bounds, stateListAnimator=@null – Ultimo_m Aug 23 '18 at 14:40
2

For Material Button, I tried the following and it worked

android:translationZ="5dp"
Muhammad Yousuf
  • 127
  • 2
  • 16
1

try this I hope it helps, because another view or layout just after your button is hiding shadow

android:layout_marginBottom="16dp"
  • When I set the background to android:color/white, the shadow appears, so I don't think it is because of another view hiding it – Jeffrey Chou Jun 14 '17 at 07:22
0

Since you're using an image, replace the <Button> tag with that of an <ImageButton>. So, your workaround code would be:

<ImageButton
        android:id="@+id/google"
        android:layout_width="270dp"
        android:layout_height="38dp"
        android:layout_marginTop="30dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/google"
        android:elevation="10dp"
        android:layout_below="@id/slogan"/>

Take note that the android:src attribute is being used here rather than the android:background attribute.

This one worked for me!

-1

try this , I hope this help you ||

android:layout_margin="5dp"