Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 632
Jeffrey Chou Avatar asked Jun 14 '17 06:06

Jeffrey Chou


People also ask

How do I set the elevation of a view in Android?

Elevation: The static component. Translation: The dynamic component used for animations. Figure 1 - Shadows for different view elevations. To set the default (resting) elevation of a view, use the android:elevation attribute in the XML layout. To set the elevation of a view in the code of an activity, use the View.setElevation () method.

Why doesn't the elevation style property work on Android?

The elevation style property on Android does not work unless backgroundColor has been specified for the element. With backgroundColor applied to instructions element: Unless I'm missing something elevation should get applied regardless of whether a view has a background color or not. React Native version: 0.33 Platform (s) (iOS, Android, or both?):

How to set the Android elevation and translationz of a button?

The default Button style under Material has a StateListAnimator that controls the android:elevation and android:translationZ properties. just add this property to your Button. you can set your own using the android:stateListAnimator property. for Understanding I set it 10dp..

Is it possible to use elevation style without background color?

Android - elevation style property does not work without backgroundColor. · Issue #10411 · facebook/react-native · GitHub Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.


3 Answers

For Material Button, I tried the following and it worked

android:translationZ="5dp"
like image 45
muhammad Yousuf Avatar answered Oct 17 '22 03:10

muhammad Yousuf


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

android:outlineProvider="bounds" – K. Sopheak

That works, thanks!

like image 63
Jeffrey Chou Avatar answered Oct 17 '22 02:10

Jeffrey Chou


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

android:layout_marginBottom="16dp"
like image 29
Parth Munjpara Avatar answered Oct 17 '22 02:10

Parth Munjpara