Surendra Shrestha Surendra Shrestha - 2 months ago 12
Android Question

How to add elevation on android api < 21

I am having some trouble adding elevation for android version < 21. Recently I found it easy to add elevation on card view but I could not achieve the same result for other views like picture view, toolbar, drawer layout, etc. Can anyone help me with it???

For example: if I want to add elevation to my drawer layout as per the material design, what should I do?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@drawable/ic_person_black_64dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android studio" />

</LinearLayout>

N J N J
Answer

One work around is that you can put all these view inside CradView

Just put LinearLayout inside CardView

For example :-

<android.support.v7.widget.CardView
    ....
    card_view:cardElevation="5dp"
    card_view:cardCornerRadius="0dp">


 <!--  other view that you want to elevate -->
 <DrawerLayout ../>  or <Toolbar  ---->

</android.support.v7.widget.CardView>
Comments