Using a cardview I realized that box-shadow is not rounded and looks awful on the phone screen, below is the example code used, and also a screenshot of the shadow, I have added more shadow for emphasis, so you can clearly see that the edges of the shadow are rectangular while the box is rounded.
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="#drawable/card"
android:elevation="5dp"
app:cardCornerRadius="5dp"
app:contentPaddingTop="8dp"
app:contentPaddingBottom="8dp"
app:contentPaddingLeft="#dimen/sideSpace"
app:contentPaddingRight="#dimen/sideSpace"
app:cardElevation="12dp">...
How can this be fixed?
You can use this drawable as a background of any view for shadow
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Drop Shadow Stack -->
<item>
<shape>
<padding android:top="0.5dp" android:right="0.5dp" android:bottom="1dp" android:left="0.5dp" />
<solid android:color="#10CCCCCC" />
<corners android:radius="8dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0.5dp" android:right="0dp" android:bottom="1dp" android:left="0.5dp" />
<solid android:color="#10CCCCCC" />
<corners android:radius="7dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#20CCCCCC" />
<corners android:radius="6dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="#30CCCCCC" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="#40CCCCCC" />
<corners android:radius="4dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="#50CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0.1dp" android:bottom="0.5dp" android:left="0.1dp" />
<solid android:color="#60CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#ffffff" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/dimen_10dp"
android:divider="#android:color/transparent"
android:dividerHeight="0.0px"
android:clipToPadding="false"
android:clipChildren="false"
app:cardElevation="10dp"
app:cardPreventCornerOverlap="false"
android:elevation="#dimen/dimen_20dp"
app:cardCornerRadius="#dimen/dimen_15dp"
>
Should have a child also have corner radius.
is this what you want?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_centerInParent="true"
android:layout_marginTop="100dp"
xmlns:tools="http://schemas.android.com/tools"
>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:elevation="0dp"
android:background="#drawable/round"
app:cardCornerRadius="100dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:contentPaddingTop="8dp"
app:contentPaddingBottom="8dp"
app:cardElevation="12dp">
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="8dp"
android:elevation="5dp"
app:cardCornerRadius="5dp"
app:contentPaddingTop="8dp"
app:contentPaddingBottom="8dp"
app:cardElevation="12dp"
android:background="#drawable/round2">
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
round.xml:-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#80000000"/>
<corners android:radius="30dp"/>
</shape>
round2.xml:-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#color/colorAccent"/>
<corners android:radius="20dp"/>
</shape>
output:-
Related
how can make this shape in android studio with xml?
a semi-circle on top of rectangle.
i want to put profile picture in top and text inside the rectangle.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<stroke android:width="1dp" android:color="#201215cc" />
<corners android:radius="8dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="100dp"
android:height="40dp"
android:bottom="4dp"
android:top="10dp">
<shape>
<solid android:color="#000" />
<corners android:radius="2dp"/>
</shape>
</item>
<item
android:width="20dp"
android:height="10dp"
android:left="10dp"
android:top="1dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000"/>
<corners
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>
</item>
<item
android:width="98dp"
android:height="38dp"
android:bottom="5dp"
android:top="11dp"
android:start="1dp">
<shape>
<solid android:color="#fff" />
<corners android:radius="2dp"/>
</shape>
</item>
<item
android:width="18dp"
android:height="10dp"
android:left="11dp"
android:top="2dp">
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff"/>
<corners
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>
</item>
</layer-list>
Output like
It can be done using ConstraintLayout.
Alight your profile pic in vertical center of top border of your rectangle and apply some start(left) margin to it.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/your_rectangle"
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginStart="20dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="20dp"
android:background="#c6c6c6"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/your_view"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="20dp"
android:background="#451245"
app:layout_constraintBottom_toTopOf="#id/your_rectangle"
app:layout_constraintStart_toStartOf="#id/your_rectangle"
app:layout_constraintTop_toTopOf="#id/your_rectangle" />
</androidx.constraintlayout.widget.ConstraintLayout>
I'm referring to the answer at https://stackoverflow.com/a/24475228/72437
The proposed answer is using drawable from Android : ?android:attr/selectableItemBackground
This is what happen when I tap on the card item. Note that, by using drawable from Android, android:state_selected="true" (when setSelected(true)) will not have any color change effect.
Hence, I would like to use my own defined drawable so that
It looks nicer.
Able to handle android:state_selected="true".
Here's my code
statelist_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/selected_background" />
<item android:state_selected="true" android:drawable="#drawable/selected_background" />
<item android:drawable="#android:color/transparent" />
</selector>
selected_background.xml
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffe1b3" />
<stroke
android:width="1px"
android:color="#fff76d3c" />
</shape>
card_row.xml
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:clickable="true"
android:foreground="#drawable/statelist_item_background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt_label_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:text="Item Number One" />
<TextView
android:id="#+id/txt_date_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="Item Number One" />
</LinearLayout>
</android.support.v7.widget.CardView>
When I long press on the card item and perform childView.setSelected(true);, here's my outcome.
All my card content (TextViews) are blocked. How can I avoid such?
Some notes regarding using android:background
Note, when you use android:background="#drawable/statelist_item_background" with CardView itself, nothing will happen.
However, if you use android:background="#drawable/statelist_item_background" with CardView's LinearLayout, you will get the following imperfect outcome.
The highlighted color doesn't cover the entire card.
Update
Seem like this is limitation of CardView - https://code.google.com/p/android/issues/detail?id=78198 Using "foreground" as workaround is not an option as it covers card content.
After experimenting for quite some time, I can pretty much conclude that this is limitation of current CardView - https://code.google.com/p/android/issues/detail?id=78198
Don't use CardView's foreground workaround. Although it is widely being proposed, it just don't work!
My suggestion is, avoid using CardView if you need a customized selector. Replace it LayerDrawable. Here's what I had done.
card.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#ffededed" />
<corners android:radius="2dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<stroke
android:width="1dp"
android:color="#ffe8e8e8" />
<corners android:radius="2dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<stroke
android:width="1dp"
android:color="#ffe1e1e1" />
<corners android:radius="2dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<stroke
android:width="1dp"
android:color="#ffdbdbdb" />
<corners android:radius="2dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<stroke
android:width="1dp"
android:color="#ffd5d5d5" />
<corners android:radius="2dp" />
</shape>
</item>
<!--
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<stroke
android:width="1dp"
android:color="#ffcfcfcf" />
<corners android:radius="2dp" />
</shape>
</item>
-->
<item>
<shape >
<solid android:color="#ffffffff" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
card_selected.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<stroke
android:width="1dp"
android:color="#ffededed" />
<corners android:radius="2dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<stroke
android:width="1dp"
android:color="#ffe8e8e8" />
<corners android:radius="2dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<stroke
android:width="1dp"
android:color="#ffe1e1e1" />
<corners android:radius="2dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<stroke
android:width="1dp"
android:color="#ffdbdbdb" />
<corners android:radius="2dp" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<stroke
android:width="1dp"
android:color="#ffd5d5d5" />
<corners android:radius="2dp" />
</shape>
</item>
<!--
<item>
<shape>
<padding android:top="0dp" android:right="0dp" android:bottom="1dp" android:left="0dp" />
<stroke
android:width="1dp"
android:color="#ffcfcfcf" />
<corners android:radius="2dp" />
</shape>
</item>
-->
<item>
<shape>
<solid android:color="#ffffe1b3" />
<stroke
android:width="1px"
android:color="#fff76d3c" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
statelist_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:state_pressed="true" android:drawable="#drawable/card_selected" /> <!-- pressed -->
<item android:state_selected="true" android:drawable="#drawable/card_selected" /> <!-- pressed -->
<item android:drawable="#drawable/card" />
</selector>
layout.xml
<!-- A CardView that contains a TextView -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:padding="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/statelist_item_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
<TextView
android:id="#+id/txt_label_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:text="Item Number One" />
<TextView
android:id="#+id/txt_date_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:text="Item Number One" />
</LinearLayout>
You will get the pretty nice outcome.
I've just tried MaterialCardView and this works:
<com.google.android.material.card.MaterialCardView
android:id="#+id/material_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/activity_vertical_margin"
app:cardCornerRadius="4dp"
app:cardBackgroundColor="#color/selector_background_color"
app:strokeWidth="2dp">
selector_background_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_activated="true"/>
<item android:color="#android:color/darker_gray" android:state_activated="false"/>
In the Activity/Fragment:
material_card_view.setOnClickListener {
it.isActivated = !it.isActivated
}
For the strokeColor though, you can't use a selector. You will have to do it programmatically if you want the stroke color to change as per selector state.
Example for state_activated:
val colorStateList: ColorStateList =
ResourcesCompat.getColorStateList(resources,
R.color.selector_stroke_color, null)!!
val colorForState =colorStateList.getColorForState(intArrayOf(android.R.attr.state_activated), colorStateList.defaultColor)
material_card_view.strokeColor = colorForState
All my card content (TextViews) are blocked. How can I avoid such?
Your blocked issue can easily be solved with some color theory knowledge. Instead of using the given peach color, you could use a variant of the peach shade with a higher transparency.
Color Code scheme in Android xxyyyyyy
xx is your transparency level, and y's are your color.
xx max value is : ff // this is fully visible
xx min value is : 00 // this is full invisible
So by playing around on the color wheel, you can get the right effect with the right amount of transparency needed for your view.
for the background not covering the entire card issue, it is due to the cornerRadius drawing limitation. There are two solutions for this:
Disable round corner for your card will resolve the problem. app:cardCornerRadius="0dp"
You can retain cardCornerRadius but you need to set app:cardPreventCornerOverlap="false"
MaterialCardView has ripple effect and doesn't need a custom background drawable as opposed to CardView.
That handled the android:state_selected and android:state_pressed for my use case.
I believe you are misunderstanding the question you referred to. The question is how to get the ripple effect:
Where I think you are just looking for click feedback?
If so, try setting the background drawable:
android:background="#drawable/statelist_item_background"
EDIT:
On you background color xml, you need to give it an alpha. It is being drawn in the front, so give it an alpha of .1f and see how that looks and go from there.
Try using color 50F76D3C Then use is it in the foreground like you were originally.
I tring to make shadow around linearLayout like on image below (on top):
I`m using layer-list with shape, but all bottom shadow owerlapping by white area (see image above). All this view looks like:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/transparent" >
<LinearLayout
android:id="#+id/listView_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="45dp"
android:background="#color/text_white">
<!-- ListView to be shown on widget -->
<ListView
android:id="#+id/listViewWidget"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- Empty view is show if list items are empty -->
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="20sp"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/widget_toolbar"
android:orientation="vertical"
android:background="#drawable/layout_card_view"/>
</FrameLayout>
How to make this?
You must use , this example has a shadow and item with background white, you can use and adjust to your preference.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Drop Shadow Stack -->
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#00CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#10CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#20CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#30CCCCCC" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#50CCCCCC" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#FFF" />
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
You can add more drop shadow how you need. Check out the color in every one.
Shadow effect using drawable :
1) Create a shadow 9 patch image.
2) Set that 9 patch image as the layout background.
use this generator to create 9 patch shadow image.
Try this...
<?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="#CABBBBBB"/>
<corners android:radius="2dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
You can use this 1 st way
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item><layer-list>
<item android:right="7dp" android:top="3dp"><shape>
</shape></item>
<item ><shape>
<gradient android:angle="270" android:endColor="#FFFFFF" android:startColor="#BABABA" />
<padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" />
</shape></item>
</layer-list></item>
</layer-list>
2nd way
You can use 9 Patch Image .
Add ll_shadow.xml file to res/drawable
<?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>
USE
<LinearLayout
android:background="#drawable/ll_shadow"/>
Use this code and create one shedow.xml file in drawable and then use as a background in your layout file to get desire output.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#your_shadow_color" />
<corners android:radius="7dp" />
</shape>
</item>
<!-- Green Top color -->
<item android:bottom="3px">
<shape android:shape="rectangle">
<solid android:color="#your_view_background_color" />
<corners android:radius="7dp" />
</shape>
</item>
</layer-list>
I am trying to get it so my listview items are elevated and show a drop shadow.
Here is my list view item layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:translationZ="5dp"
android:cropToPadding="false">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:background="#color/background_white"
android:layout_alignBottom="#+id/PostUpvote"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:translationZ="2dp"
/>
and here is my ListView layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
android:background="#color/background_white"
android:dividerHeight="10sp"
android:translationZ="2dp"/>
Any help would be appreciated.
If you are targeting version 21 you can use
android:elevation="10dp"
If not then you may need to create the drop shadow effect yourself in a drawable. For example this one creates drop shadow on right and bottom sides.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Adding border layers simulating drop shadow -->
<item>
<shape>
<padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="#00494949" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="#10494949" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="#20494949" />
</shape>
</item>
<item>
<shape>
<padding android:top="0dp" android:right="1dp" android:bottom="1dp" android:left="0dp" />
<solid android:color="#30494949" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" android:right="1dp" android:bottom="1dp" android:left="1dp" />
<solid android:color="#70494949" />
</shape>
</item>
<item>
<shape>
<solid android:color="#android:color/white" />
</shape>
</item>
Save it in drawable as my_drop_shadow.xml and use it in any View with android:background="#drawable/my_drop_shadow"
You can adjust shadow direction by changing padding values and add elevation effect by controlling transparency.
I have created chat bubble with drawable which looks fine, now I want to add shadow effect below the drawable so it make 3d effect.I don't want to use 9-pitch image. I just want to know how i can add shadow effect on this drawable. my code is
----right_bubble_chat_drawable
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="15dp" />
<solid android:color="#color/chatrightbubbleColor" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<size
android:height="#dimen/normal_button_height"
android:width="#dimen/normal_button_width" />
--- for corner pointer 'chat_laftarraow'
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<rotate
android:fromDegrees="90"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="50%" >
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="86%" >
<shape
android:shape="rectangle" >
<stroke android:color="#00aaef" android:width="1dp"/>
<solid
android:color="#00aaef" />
</shape>
</rotate>
</rotate>
</item>
</layer-list>
------- I am using them like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View
android:id="#+id/left_chatArror"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginTop="6dp"
android:background="#drawable/chat_laftarraow"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="220dp"
android:layout_marginRight="-3dp"
android:orientation="horizontal"
android:layout_toLeftOf="#+id/left_chatArror"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:background="#drawable/right_bubble_chat_drawable">
</RelativeLayout>
</RelativeLayout>
Please suggest me how can I add shadow effect below the bubble as in the image below
You can try by implementing a layer-list that will act as the background for the LinearLayout and add your view inside this.
Quote from an answer to this question:
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 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"/>
EDIT
You can create seprate xml for creating gray image like thsis:
----right_bubble_shdw_chat_drawable
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="15dp" />
<solid android:color="#android:color/darker_gray" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<size
android:height="#dimen/normal_button_height"
android:width="#dimen/normal_button_width" />
--- for corner pointer 'chat_laftarraow_shdw'
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<rotate
android:fromDegrees="90"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="50%" >
<rotate
android:fromDegrees="45"
android:toDegrees="45"
android:pivotX="-40%"
android:pivotY="86%" >
<shape
android:shape="rectangle" >
<stroke android:color="#android:color/darker_gray" android:width="1dp"/>
<solid
android:color="#android:color/darker_gray" />
</shape>
</rotate>
</rotate>
</item>
</layer-list>
------- I am using them like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View
android:id="#+id/left_chatArror"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginTop="6dp"
android:background="#drawable/chat_laftarraow"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="220dp"
android:layout_marginRight="-3dp"
android:orientation="horizontal"
android:layout_toLeftOf="#+id/left_chatArror"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:background="#drawable/right_bubble_chat_drawable">
</RelativeLayout>
<View
android:id="#+id/left_chatArrorShdw"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:background="#drawable/chat_laftarraow_shdw"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="220dp"
android:layout_marginRight="-3dp"
android:orientation="horizontal"
android:layout_toLeftOf="#+id/left_chatArror"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:background="#drawable/right_bubble_shdw_chat_drawable">
</RelativeLayout>
I am using this drawable/shadow.xml file
<!-- Drop Shadow Stack -->
<item>
<shape>
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#00CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape>
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#10CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape>
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#20CCCCCC" />
<corners android:radius="3dp" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#android:color/white" />
<corners android:radius="3dp" />
</shape>
</item>
Note :- Root layout should be layer-list. Inside it add items.
Add round_big_green_down.xml as background to your position.
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/**round_big_green_down**"
android:paddingBottom="#dimen/button_margin_100"/>
res/drawable/round_big_green_down.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Drop Shadow Stack -->
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#00000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#01000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#02000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#03000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#04000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#05000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#06000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#06000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#06000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<item>
<shape>
<padding android:top="1.5dp" />
<solid android:color="#06000000" />
<corners android:radius="#dimen/button_radius" />
</shape>
</item>
<!-- White Top color -->
<item android:top="#dimen/button_margin_5">
<shape >
<gradient
android:angle="90"
android:endColor="#color/primary_color_50"
android:startColor="#color/primary_color_500" />
<corners
android:topLeftRadius="#dimen/button_radius"
android:topRightRadius="#dimen/button_radius"/>
</shape>
</item>
</layer-list>
res/values/dimens/
<dimen name="button_radius">25dp</dimen>