Jeffrey Chou
Jeffrey Chou

Reputation: 331

Android button elevation shadow not working

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?

Upvotes: 6

Views: 17086

Answers (5)

Surajkaran Meghwanshi
Surajkaran Meghwanshi

Reputation: 676

try this , I hope this help you ||

android:layout_margin="5dp"

Upvotes: -1

Muhammad Yousuf
Muhammad Yousuf

Reputation: 178

For Material Button, I tried the following and it worked

android:translationZ="5dp"

Upvotes: 3

Jeffrey Chou
Jeffrey Chou

Reputation: 331

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

android:outlineProvider="bounds" – K. Sopheak

That works, thanks!

Upvotes: 10

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!

Upvotes: 0

Parth Munjpara
Parth Munjpara

Reputation: 82

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

android:layout_marginBottom="16dp"

Upvotes: 1

Related Questions