Medo Zeus
Medo Zeus

Reputation: 99

android add shadow below view

i just try to add shadow to my LinearLayout to make my View act like its over other view just like this image facebook messenger

image

i try this xml as background from my View but its seems like a line not shadow

<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="@color/black_alpha_12" 
android:endColor="@android:color/transparent" android:angle="90.0" />
</shape>

and i try many xml code in stackoverflow no one help me to make shadow like what i want

Upvotes: 2

Views: 6672

Answers (6)

Abraham Mathew
Abraham Mathew

Reputation: 2146

Attribute elevation is only used in API level 21 and higher For example, some tags can be unused too, such as the new element in layouts introduced in API 21.

Set the elevation property in xml to say 10dp or 8dp android:elevation="10dp"

Upvotes: 0

Ratilal Chopda
Ratilal Chopda

Reputation: 4220

Try this

getSupportActionBar().setElevation(10);

Upvotes: 1

Rajpal
Rajpal

Reputation: 57

Use <layer-list> for set background of view. Like this

<TextView
  android:id="@+id/tv_item"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/keyboard_btn_bg"
    />

keyboard_btn_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="130dp"
                android:height="25dp" />
            <corners android:radius="8dp" />

            <padding
                android:bottom="2dp"/>
            <stroke
                android:width="0dp"
                android:color="#20000000" />
            <solid android:color="#20000000" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <size
                android:width="160dp"
                android:height="25dp" />
            <corners android:radius="8dp" />
            <padding
                android:top="8dp"
                android:bottom="8dp"
                android:left="8dp"
                android:right="8dp"
                />
            <stroke
                android:width="1dp"
                android:color="#808080" />
            <solid android:color="#ffffff" />
        </shape>
    </item>

Upvotes: -1

user3876059
user3876059

Reputation: 90

HI for shadow effect you can you cardview or view.setElevation(10); both will work.

ref-How to add shadow on CardView aligned to bottom of parent

ref-https://material.io/guidelines/material-design/elevation-shadows.html#

Upvotes: 0

InsaneCat
InsaneCat

Reputation: 2161

Try this

getSupportActionBar().setElevation(10);

Another reference i had answered here first Example

Hope this may helps you

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69689

Use

getSupportActionBar().setElevation(10);

But as per Your Above Comment in my app i made an LinearLayout as ActionBar did this will work

Than Use

YourView.setElevation(10);

Upvotes: 3

Related Questions