Android button elevation shadow not working
Asked Answered
E

5

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?

Euhemerize answered 14/6, 2017 at 6:54 Comment(12)
#27080838Knapsack
you can set android:stateListAnimator="@null" for buttonLinks
elevation is only used in API level 21 and higherChekhov
@IntelliJAmiya As I said, I already looked through the forums. Setting stateListAnimator to null for the button and clipToPadding to false for the layout still doesn't make the shadow appear. Am I doing something wrong?Euhemerize
I ran my emulator at API level 25. I also set the minimum API level in gradle to 21.Euhemerize
add in cardview and give a elevation to cardview.Reamy
@Jeffrey Chou Background covers elevation view as well. That's why.Chapiter
@kevanaghera I tried your workaround and it works. Thanks. But, I am wondering if that is the only workaround or the easiest. It is a hassle to put each button in a card view.Euhemerize
@NitinPatel So besides putting each button in a cardview, how would I work around that?Euhemerize
Try this link if it works:- https://mcmap.net/q/1770349/-imagebutton-elevation-issueChapiter
use android:outlineProvider="bounds"Wallaby
Damn it, I assumed in Android Studio in 2021 I won't have such issues :((Salts
E
10

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

android:outlineProvider="bounds" – K. Sopheak

That works, thanks!

Euhemerize answered 23/6, 2017 at 9:12 Comment(2)
For me this one resulted in strange background behaviour but android:outlineProvider="paddedBounds" was the key.Ilbert
for me it works only with: elevation, translationZ, outlineProvider=bounds, stateListAnimator=@nullKingdom
P
3

For Material Button, I tried the following and it worked

android:translationZ="5dp"
Polythene answered 17/1, 2021 at 16:6 Comment(0)
R
1

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

android:layout_marginBottom="16dp"
Regelate answered 14/6, 2017 at 7:19 Comment(1)
When I set the background to android:color/white, the shadow appears, so I don't think it is because of another view hiding itEuhemerize
C
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!

Chitter answered 14/6, 2017 at 8:5 Comment(0)
H
-1

try this , I hope this help you ||

android:layout_margin="5dp"
Heterogamete answered 21/6, 2022 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.