Android View shadow
I know this question has already been answered but I want you to know that I found a drawable
on Android Studio
that is very similar to the pics you have in the question:
Take a look at this:
android:background="@drawable/abc_menu_dropdown_panel_holo_light"
It looks like this:
Hope it will be helpful
Edit
The option above is for the older versions of Android Studio
so you may not find it. For newer versions:
android:background="@android:drawable/dialog_holo_light_frame"
Moreover, if you want to have your own custom shape, I suggest to use a drawing software like Photoshop
and draw it.
Don't forget to save it as .9.png
file (example: my_background.9.png
)
Read the documentation: Draw 9-patch
Edit 2
An even better and less hard working solution is to use a CardView
and set app:cardPreventCornerOverlap="false"
to prevent views to overlap the borders:
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="2dp"
app:cardElevation="2dp"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp">
<!-- your layout stuff here -->
</android.support.v7.widget.CardView>
Also make sure to have included the latest version in the build.gradle
, current is
compile 'com.android.support:cardview-v7:26.0.0'
How to add a shadow above a view in android
Here are some solutions for this problem - choose your best:
- On StackOverflow, in this post from 22 October 2012, you would read:
There is no such attribute in Android, to show a shadow. But possible ways to do it are:
- Add a plain LinearLayout with grey color, over which add your actual layout, with margin at the bottom and right equal to 1 or 2 dp.
- Have a 9-patch image with a shadow and set it as the background to your Linear layout.
and
There is also another solution to the problem by implementing a layer-list that will act as the background for the LinearLayoout.
Add background_with_shadow.xml file to
res/drawable
. Containing:<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape
android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
<corners android:radius="5dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>Then add the layer-list as background in your LinearLayout.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_with_shadow"/>
You can also read: http://odedhb.blogspot.com/2013/05/android-layout-shadow-without-9-patch.html
- Another post from StackOverflow, how to set shadow to a View in android?, gives you another solution (using two views that form the shadow.):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#CC55CC">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow>
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="@string/hello" />
</LinearLayout>
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#55000000"/>
</TableRow>
</TableLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_marginLeft="5dp"
android:background="#55000000"/>
</LinearLayout>
</FrameLayout>
- You can also use specific drawable form Android resources to mimic a shadow effect. Look at: Android View shadow or just read a post below:
I'm using Android Studio 0.8.6 and I couldn't find:
android:background="@drawable/abc_menu_dropdown_panel_holo_light"
so I found this instead:
android:background="@android:drawable/dialog_holo_light_frame"
and it looks like this:
If you're interested in clean Material Design effect, read some documentation like below:
- Android L Developer Preview: Views and
Shadows - Defining Shadows and Clipping
Views
How can we set shadow effect for View in Android?
You can create your own "shadow view"
<View
android:id="@+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_gravity="bottom"
android:background="@drawable/shadow_gradient" />
and add the shadow drawable to it:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:endColor="@android:color/transparent"
android:startColor="#8f000000" />
</shape>
The height is up to you, to experiment and see what works best for you as is the angle and start colour of the drawable. You just need to align this view layout to any view layout you want to have a shadow and you are good.
how to set shadow to a View in android?
There are a simple trick, using two views that form the shadow.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="#CC55CC">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0">
<TableRow>
<LinearLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="@string/hello" />
</LinearLayout>
<View
android:layout_width="5dp"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#55000000"/>
</TableRow>
</TableLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:layout_marginLeft="5dp"
android:background="#55000000"/>
</LinearLayout>
</FrameLayout>
Hope this help.
android add shadow below view
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);
How to add shadow on CardView aligned to bottom of parent
Well margin don't help much, so I put padding on main container and remove all those shadow properties because the android:elevation=""
is what is doing the job.
Here is some clear code, that is working for this need:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:clipToPadding="false"
android:clipChildren="false"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--rest of the code-->
<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:clipToPadding="false"
android:clipChildren="false"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="8dp"
android:divider="@android:color/transparent"
android:dividerHeight="0.0px"
android:clipToPadding="false"
android:clipChildren="false"
card_view:cardElevation="10dp"
card_view:cardPreventCornerOverlap="false">
<TextView
android:layout_width="match_parent"
android:padding="20dp"
android:layout_height="wrap_content"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed
do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
And an image on how this looks:
How to show shadow around the linearlayout in Android?
There is no such attribute in Android, to show a shadow. But possible ways to do it are:
Add a plain LinearLayout with grey color, over which add your actual layout, with margin at bottom and right equal to 1 or 2 dp
Have a 9-patch image with a shadow and set it as the background to your Linear layout
Add shadow to bottom of view
Try using below line in the linearlayout
under which your cardview
is sitting.
android:elevation="8dp"
Let the cardElevation
part in your cardView
be as it is.
card_view:cardElevation="10dp"
card_view:cardPreventCornerOverlap="false"
Also just a suggestion :
If you're using android:layout_weight
in both child linearLayout
put android:weightSum="2"
in your parent linearLayout
as mentioned in answer given by @Ming Leung here
For more information on android:weightSum="2"
you can refer to answers on this page
This page would help you for elevation
in cardview
Hope that helps.
Android elevation not showing a shadow
I've been playing around with shadows on Lollipop for a bit and this is what I've found:
- It appears that a parent
ViewGroup
's bounds cutoff the shadow of its children for some reason; and - shadows set with
android:elevation
are cutoff by theView
's bounds, not the bounds extended through the margin; - the right way to get a child view to show shadow is to set padding on the parent and set
android:clipToPadding="false"
on that parent.
Here's my suggestion to you based on what I know:
- Set your top-level
RelativeLayout
to have padding equal to the margins you've set on the relative layout that you want to show shadow; - set
android:clipToPadding="false"
on the sameRelativeLayout
; - Remove the margin from the
RelativeLayout
that also has elevation set; - [EDIT] you may also need to set a non-transparent background color on the child layout that needs elevation.
At the end of the day, your top-level relative layout should look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/block"
android:gravity="center"
android:layout_gravity="center"
android:background="@color/lightgray"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:clipToPadding="false"
>
The interior relative layout should look like this:
<RelativeLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:background="[some non-transparent color]"
android:elevation="30dp"
>
Related Topics
Add a Search Filter on Recyclerview with Cards
Switch Keyboard Profile Programmatically
Work Around Canvas.Clippath() That Is Not Supported in Android Any More
Cmusphinx Pocketsphinx - Recognize All (Or Large Amount) of Words
Android Listview with Checkbox and All Clickable
Custom Seekbar (Thumb Size, Color and Background)
Change the Font of Tab Text in Android Design Support Tablayout
Android Imageview: Setting Drag and Pinch Zoom Parameters
How to Repeat a Method Every 10 Minutes After a Button Press and End It on Another Button Press
Handling Buttons Inside Android Notifications
How to Handle Outofmemoryerror
How to Send JSON Object to Server Using Volley in Android
Android Google Maps, How to Make Each Marker Infowindow Open Different Activity
One Activity and All Other Fragments