I have this minimal Android Studio project in just one button. I assign a shadow to the button with:
android:elevation="3dp" android:translationZ="3dp" android:stateListAnimator="@null"
and I see a shadow on the Android Studio Design tab. But I also get a warning in the xml editor
The attribute ... is used only in API level 21 and higher (current min is 16)
Indeed, the shadow of the button is displayed only in emulators with API level 21 or higher , and it is displayed flat, with no shadow at all in emulators with API level lower than 21 .
So, the specific question is: how can I emulate the shadow effect in API lower than 21 .
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" xmlns:app="http://schemas.android.com/apk/res-auto"> <LinearLayout android:id="@+id/main_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:weightSum="1"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:layout_weight="2.24"> <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:layout_marginBottom="20dp" android:elevation="5dp" android:translationZ="5dp" android:stateListAnimator="@null" android:background="@android:color/holo_green_light" android:text="BUTTON"/> </RelativeLayout> </LinearLayout> </ScrollView>
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Button showing a shadow in API 22 : 
The same button does not show a shadow, in API 16 : 
source share