Android elevation not showing shadow on Button
36,866
The default
Button
style underMaterial
has aStateListAnimator
that controls theandroid:elevation
andandroid:translationZ
properties.
just add this property to your Button
. you can set your own using the android:stateListAnimator
property.
android:stateListAnimator="@null"
full code :
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:elevation="2dp"
android:translationZ="2dp"
android:stateListAnimator="@null"
android:background="@android:color/holo_green_light"
android:text="BUTTON">
UpDate :
for Understanding I set it 10dp..
xml code :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="20dp"
android:elevation="10dp"
android:translationZ="10dp"
android:stateListAnimator="@null"
android:background="@android:color/holo_green_light"
android:text="BUTTON"/>
</RelativeLayout>
Related videos on Youtube
Comments
-
Ramiro almost 2 years
Can't get button shadows to show up.
Stripped down my code to the minimal example:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView 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"> <LinearLayout android:id="@+id/main_layout" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:layout_marginLeft="10dp"> <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginLeft="20dp" android:elevation="10dp" android:translationZ="10dp" android:background="@android:color/holo_green_light" android:text="BUTTON"/> </RelativeLayout> </LinearLayout> </ScrollView>
I need that layout structure.
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Shadows is visible in Android Studio designer:
But not shown at runtime:
Tested on:
- Genymotion Google Nexus 5X - Marshmallow 6.0.0 - API 23
- Genymotion Google Nexus 5 - Lollipop 5.1.0 - API 22
- Genymotion Samsung Galaxy S2 - Jelly Bean 4.1.1 - API 16
- Hardware device, Samsung Galaxy S5 mini - KitKat 4.4.2 - API 19
All same result.
I'm using:
- Android Studio 2.1.2
- minSdkVersion 16
- targetSdkVersion 24
Please create a new project in Andriod Studio, Empty Activity template, then copy and paste that code into
activity_main.xml
andMainActivity.java
so you can test and tell me.-
Harshad Pansuriya@jankigadhiya yes You Are Right and also in Material design
android:elevation="2dp"
andandroid:translationZ="2dp"
is only work when you setandroid:stateListAnimator="@null"
otherwise it is take deafult property. -
Janki GadhiyaThere is no such property
layout_topLeft
for anyView
..!!
-
Aba over 6 years"Unexpected namespace prefix.."
-
sziraqui about 6 yearsapp: prefix is not available in button
-
Khan about 6 yearsOh my god.. You have to add namespace first. Dont downvote if you dont know. Tell me to add more specification. I will explain it.
-
sziraqui about 6 yearsEven if you add app namespace, default
stateListAnimator
overrideselevation
in both namespaces. I wrote the previous comment in a hurry