How to implement shadows like the image below?
I try many ways as CardView, elevation, many custom views, drawable. But that was different from the wanted shadows.
CardView
elevation of CardView is ambient light + key light. But, I want just ambient light elevation.
I Want this shadow.
Starting with API 28 you can set the color of the shadows.
If you want the ambient shadow only, then you need to set the color of the key light(aka spot light) to transparent.
You can do so by adding the following to your view tag in xml:
android:outlineAmbientShadowColor="#android:color/transparent"
Now only key/spot light will show.
and for ambient shadow
android:outlinesSpotShadowColor="#android:color/transparent"
Also check
setOutlineAmbientShadowColor() and setOutlinePostShadowColor()
to do it programmatically
You can also set them to other colors if you don't want a black shadow.
Related
I am making effort to change my cardView background color to a different color other than the default color set in style with the colorSurface property.
I am aware colorSuface is responsible for a cardView color, but it is surprisingly difficult to change it color in XML.
I have tried the following singly and together but there is no effect.
android:background="#color/purple_200"
app:cardBackgroundColor="#color/purple_200"
android:backgroundTint="#color/purple_200"
I will appreciate help.
Here is my styles code
<item name="colorSurface">#color/app10</item>
The reason I don't want to change the colorSurface property is because, that is the default desired color I want for all cardView but in this particular layout, I want to style my row of cardView widgets with tones of a color, unfortunately, it is not responding.
I figured out the issue was the color set to an imageView inside the cardView was overlaying the color of the cardView.
Another possible cause of this kind of behaviour is if the cardView was inside a layout whose elevation (layout) is possibly greater than the cardView.
Thanks #Mike M for asserting that cardView doesn't disappoint in this case.
Want this kind of shadow effect with android card view except for white background with cardview property, neither use with the canvas draw mechanism nor 9 patch image mechanism
want to use only drawable shape or cardview properties. TIA
EDIT: There is this github project you can use to get custom shadow for CardView. Just implement it in your project and use this instead of androidx components. Link: https://github.com/nvl2val/CardView/tree/master
2nd EDIT: Also another workaround for this issue. Read this thread: Android change Material elevation shadow color
I don't really get your question but CardView itself has a lot of properties you can play with. Check this documentation for more: https://developer.android.com/reference/androidx/cardview/widget/CardView
Using elevation will give you shadow by default so if you don't want a shadow on objects with a white background you need to set the elevation to 0. Here you can play with
android:elevation = "10dp"
app:cardElevation = "10dp"
and see which one does the job better for you. Also, try adding some padding to your parent layout so that CardView has some space around. All this and maybe more you can find in this answer: https://stackoverflow.com/a/46374054/14759470
I want to change Floating Action button shadow colour from black/grey to colorprimary/custom color of shadow ** shadow example like below image with **center blue FAB button with light blue shadow not grey shadow. But we can change the FAB button background color. But as you can see in image there is blue shadow of FAB button.I want to achive that thing.
try this :: app:backgroundTint="#color/colorAccentGrey"
where colorAccentGrey = YourColor
and put xmlns:app="http://schemas.android.com/apk/res-auto" at the beginning of the XML if you forggt,
and for Remove shadow :: app:elevation="0dp"
Hope this will help you.. :)
I think you have 2 options:
as #Uttam said, change the elevation of the FAB widget
to make a custom design and embed it as an image in your layout as shown here http://androidgifts.com/android-material-design-floating-action-button-tutorial/
None of the answer worked for me. So i worked this. anyhow it will give shadow like effect that enough for me
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add_black"
app:elevation="0dp" // disable outside shawdow
app:borderWidth="30dp" // make borderwidth to 25dp or height of fab
app:backgroundTint="#00E5FF" // now you will see only this color with shawdow
android:backgroundTint="#00E5FF" // since border is 30dp u ll not see this color and if you want to check reduce border width to 25dp then ull see this color in center as a small dot.
/>
A simple solution is to remove the stroke(border-width), the default value is 0.5dp change it to 0dp, that is
app:borderWidth="0dp"
Refer Regular and mini FAB key properties in Material Design documentation!
Link - https://material.io/components/buttons-floating-action-button/android
Using a background color for a view with some alpha (e.g. #99fe0038) and some elevation on API 21 reveals two circles: one for the view itself and another inside:
Elevation and background color are set via code:
view.setElevation(getResources().getDimensionPixelSize(R.dimen.fab_elevation_lollipop));
view.setBackgroundColor(Color.parseColor("#99fe0038"));
Without setting elevation or with using an opaque color everything looks like expected.
Is it an Android bug or have I missed something here?
Removing shadow effect worked for me.
FAB.setShadow(false);
FAB.setBackgroundColor(getResources().getColor(R.color.fab_transparent));
Is there a way to make an android button’s background translucent within xml code,
So the button has no background drawable?
android:background="#android:color/transparent"
You can also set your own colors:
android:background="#80000000"
The first two hex characters represent opacity. So #00000000 would be fully transparent. #80000000 would be 50% transparent. #FF000000 is opaque.
The other six values are the color itself. #80FF8080 is a color I just made up and is a translucent sort of pinkish.
The best way is to create a selector.
Set the background to the standard android transparent color.
In Code:
myButton.setBackground(getResources().getColor(android.R.color.transparent));
In xml:
android:background="#android:color/transparent"