the CardView ( android.support.v7.cardview ) stays white even though I set a backround drawable via android:backround - The documentation gives me the feeling that it should work. No Idea what I am doing wrong here.
I know this is an old question, but I have a simple solution - just make the first child of your CardView an ImageView and specify the scale type to fitXY. You can get rid of the extra CardView padding by setting cardElevation and cardMaxElevation to 0dp:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.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="200dp"
app:cardCornerRadius="4dp"
app:cardElevation="0dp"
app:cardMaxElevation="0dp">
<ImageView
android:src="#drawable/your_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
<... your layout
.../>
</android.support.v7.widget.CardView>
for drawable or color just use:
cvSellerShopp.setBackgroundResource(R.drawable.ic_action_add_image);
for color use:
cvSellerShopp.setCardBackgroundColor(R.color.colorPrimary);
but this one does not produce the intended results
Make Cardview will host one viewgroup for eg relative layout in this case and then simply set any background to relative layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/list_container_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<!--Add cardview contents-->
</RelativeLayout>
</android.support.v7.widget.CardView>
I got this working by adding a linearlayout in the cardview and then setting cardPreventCornerOverlap to false in the cardview.
<android.support.v7.widget.CardView
android:id="#+id/result_cv"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="4dp"
app:cardCornerRadius="16dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient"
android:gravity="center"
>
<!-- your views -->
</LinearLayout>
</android.support.v7.widget.CardView>
The command I used was:
cardView.setBackgroundResource(R.drawable.card_view_bg);
where card_view_bg is a custom XML resource file.
If you have some layouts inside the card view and these layouts are overlapping with the margins of the card view then you might need a separate custom background resource file for the layouts like the one I used for the card view background itself.
Try this
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/list_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="4dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="#+id/list_container_bg"
android:layout_width="match_parent"
android:background="#drawable/yourbackground"
android:layout_height="match_parent">
<!--Add cardview contents-->
</RelativeLayout>
</android.support.v7.widget.CardView>
card_view:cardBackgroundColor="#android:color/holo_green_dark
here you can set any color or make it transparent by giving color transparent
and if u wand some drawable like image or svg put RelativeLayout background
You can use LinearLayout or Relative layout inside CardView and set drawable background it like below :
<android.support.v7.widget.CardView
android:id="#+id/card6"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/card4"
app:cardCornerRadius="10dp"
app:cardElevation="4dp"
app:cardMaxElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/card_bg"
android:padding="10dp">
<ImageView
android:id="#+id/card6Image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher_round" />
<TextView
android:id="#+id/card6Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="Daily Check In"
android:textColor="#ffffff"
android:textSize="15sp" />
<TextView
android:id="#+id/card6Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="Just Check in Daily and Earn Credits"
android:textColor="#ffffff"
android:textSize="10sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Below is my Drawable resource file :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
<corners
android:radius="0dp"/>
you can set foreground as follow for the card view to set custom bg
android:foreground="#drawable/bg"
and here is the transparent bg.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#d9d9d9" />
<corners android:radius="4dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
You have 2 possible solutions.
Setting background programmatically:
cardView.setBackgroundResource(R.drawable.background)
Add some child to cardview that will fill cardview's bounds and set background on it:
<android.support.v7.widget.CardView
...>
<ConstraintLayout
android:background="#drawable/background"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v7.widget.CardView>
This way you can also give images some shape that you can configure by the shape of cardview. Disadvantage is that it is bad for performance.
Try this code:
cardView.setForeground(getResources().getDrawable(R.drawable.your_drawable));
sCard.setBackgroundResource(R.drawable.scard_background);
if you want change background color on cardView just add this code on your cardview XML
app:cardBackgroundColor="#color/whatever_color_you"
You can simply use the below line in your xml file.
app:cardBackgroundColor="#drawable/your_custom_theme_here"
Related
I have a horizontal recyclerView of images. I want to set a background color that covers the recyclerView.
I want to:
I made with my xml code:
I've added a view to the RecyclerView area. Background color gave it there. When I write it like this in the code, the background color is under the recyclerView:
gradient background:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:centerColor="#f1f1f1"
android:endColor="#FFF8F8F6"
android:startColor="#color/white" />
</shape>
xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#color/colorAccent" />
<FrameLayout
android:id="#+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomView"
android:layout_margin="5dp"
android:layout_below="#+id/toolbar"
android:layout_gravity="bottom" />
<View
android:id="#+id/bottomView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignStart="#+id/images"
android:layout_alignLeft="#+id/images"
android:layout_alignTop="#+id/images"
android:layout_alignEnd="#+id/images"
android:layout_alignRight="#+id/images"
android:layout_alignBottom="#+id/images"
android:layout_alignParentBottom="true"
android:background="#drawable/gradient_white" />
<ImageView
android:id="#+id/sendButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:contentDescription="#null"
android:elevation="5dp"
android:gravity="center"
android:padding="14dp"
android:scaleType="center"
app:srcCompat="#drawable/ic_baseline_send_24" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/images"
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_gravity="bottom"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_alignParentBottom="true"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp" />
</RelativeLayout>
first of all rearrange order of your Views - first should be RecyclerView, then covering background, then send icon. Views placed in XML are drawn in order of declaration, treat them like layers in your case (and for the future: be aware of translationZ and elevation XML attributes)
and for placing covering "background" View (in fact this is foreground, "above" RecyclerView) use this:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/images" ....
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#80FFFFFF"
android:layout_alignBottom="#id/images"
android:layout_alignTop="#id/images"
android:layout_alignLeft="#id/images"
android:layout_alignRight="#id/images"/>
<ImageView
android:id="#+id/sendButton" ...
"covering" View may have 0dp size for better measuring performance, still all android:layout_align... declarations make it stretch to the size of RecyclerView (and as later-declared this View will be drawn after RecyclerView, so on top of it)
just adjust your android:background, use some gradient drawable with transparency
edit: gradient drawable file (put in drawable folder)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#00ffffff"
android:centerColor="#00ffffff"
android:centerY="80%"
android:endColor="#fff"
android:angle="0"
android:dither="true"/>
</shape>
set it up as
android:background="#drawable/your_file_name_of_above_shape"
My xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/recyclerMainCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
app:cardElevation="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imgRecyclerMain"
android:layout_width="match_parent"
android:layout_height="100dp"
android:fitsSystemWindows="true"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical"
android:weightSum="5">
<TextView
android:id="#+id/txtRecyclerMainTheme"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_weight="2"
android:orientation="horizontal">
<TextView
android:id="#+id/txtRecyclerMainNumberCommunity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:textSize="14sp" />
<TextView
android:id="#+id/txtRecyclerMainNumberArticles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
That is my recycler_main_themes.xml which in inside a recycler view to display themes. The behavior of the image I'm looking for is to have the card with both left and right borders with 8dp radius, so I've put inside the MainActivity this:
cardView.setBackgroundResource(R.drawable.card_view_border);
And this is the card_view_border.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white" />
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp" />
</shape>
But when the app runs, before the image is displayed, the cardView has both top borders with the radius then after the image is downloaded, the imageView overlaps the cardView and the card looks rectangular. Picture of how it is:
What am I missing to display it properly?
Edit: just to complement, I've tried many things such as changing the ImageView scaleType to fitXY, set setPreventCornerOverlap(false) on CardView, changing cardElevation on CardView and putting elevation on ImageView, but none succeeded.
I want to put the CardView inside Rounded LinearLayout. but I got my layout still square, but If I put the Another view, it is rounded.
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/rounded">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:cardPreventCornerOverlap="false" />
</LinearLayout
Here my rounded layout
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white"/>
<stroke
android:width="3dp"
android:color="#color/white" />
<corners android:radius="8dp"/>
</shape>
I try use app:cardPreventCornerOverlap="false" but still doesn't work.
How to my card become rounded because it is inside rounded parent?
I have tried another way.
I change the parent using CardView and implementation app:cardCornerRadius
<android.support.v7.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:cardPreventCornerOverlap="false" />
</LinearLayout
But still same, CardView child covering CardView parent
Give equal radius to CardView.
<LinearLayout
...>
<android.support.v7.widget.CardView
app:cardCornerRadius="8dp"
... />
</LinearLayout
do something like this ..
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:cardCornerRadius="130dp"/>
</LinearLayout
Try this out
<LinearLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/rounded"
android:padding="10dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardCornerRadius="8dp"
app:cardElevation="0dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true" />
</LinearLayout>
you can adjust app:cardElevation and android:padding as per your need.
cheers.
I want to have 3 circular buttons horizontally with equal margins. I tried setting programmatically but I get NPE since view was not drawn.
How can I do it in layout file? I tried below but is not circle.
Place 3 buttons in a LinearLayout to occupy equal amount of space
Please help.
Use like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
</LinearLayout>
Apply style to your buttons
You can try this.
circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="#FF0000"/>
</shape>
Put this inside the drawable folder and set this as background of your buttons (added like in the accepted answer of the link).
Hope this helps
I am new to Android and this is my first question here.
I am trying to add a colored vertical border at the beginning of the cardview. How can I achieve it on xml ? I tried adding it with empty textview but it is messing up the whole cardview itself. Please check the picture link posted below for example.
activity_main.xml
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:contentPadding="16dp"
card_view:cardElevation="2dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Content here" />
</LinearLayout>
</android.support.v7.widget.CardView>
Many thanks
Start From the material design update, it support app:strokeColor and also app:strokeWidth. see more
to use material design update. add following code to build.gradle(:app)
dependencies {
// ...
implementation 'com.google.android.material:material:1.0.0'
// ...
}
and Change CardView to MaterialCardView
try doing:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardElevation="2dp"
card_view:cardCornerRadius="5dp">
<FrameLayout
android:background="#FF0000"
android:layout_width="4dp"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Content here" />
</LinearLayout>
</android.support.v7.widget.CardView>
this removes the padding from the cardview and adds a FrameLayout with a color. You then need to fix the padding in the LinearLayout then for the other fields
Update
If you want to preserve the card corner radius create card_edge.xml in drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#F00" />
<size android:width="10dp"/>
<padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
<corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp"
android:topRightRadius="0.1dp" android:bottomRightRadius="0.1dp"/>
</shape>
and in the frame layout use android:background="#drawable/card_edge"
As the accepted answer requires you to add a Frame Layout, here how you can do it with material design.
Add this if you haven't already
implementation 'com.google.android.material:material:1.0.0'
Now change to Cardview to MaterialCardView
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="2dp"
app:strokeWidth="1dp"
app:strokeColor="#color/black">
Now you need to change the activity theme to Theme.Material. If you are using Theme.Appcompact I will suggest you to move to Theme.Material for future projects for having better material design in you app.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
I think this solution may not be efficient but it serves the purpose and adds flexibility with the border width.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="40dp"
android:layout_gravity="center"
card_view:cardBackgroundColor="#color/some_color"
card_view:cardCornerRadius="20dp"
card_view:contentPadding="5dp"> <!-- Change it to customize the border width -->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
card_view:cardCornerRadius="20dp"
card_view:contentPadding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Add your UI elements -->
</RelativeLayout>
</android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>
CardView extends FrameLayout, so it support foreground attribute. Using foreground attribute can also add border easily.
layout as follows:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/link_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="#drawable/bg_roundrect_ripple_light_border"
app:cardCornerRadius="23dp"
app:cardElevation="0dp">
</androidx.cardview.widget.CardView>
bg_roundrect_ripple_light_border.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/ripple_color_light">
<item>
<shape android:shape="rectangle">
<stroke
android:width="0.5dp"
android:color="#DDDDDD" />
<corners android:radius="23dp" />
</shape>
</item>
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="23dp" />
<solid android:color="#color/background" />
</shape>
</item>
</ripple>
my solution:
create a file card_view_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white_background"/>
<stroke android:width="2dp"
android:color="#color/red" />
<corners android:radius="20dip"/>
</shape>
and set programmatically
cardView.setBackgroundResource(R.drawable.card_view_border);
I would like to improve the solution proposed by Amit. I'm utilizing the given resources without adding additional shapes or Views. I'm giving CardView a background color and then nested layout, white color to overprint yet with some leftMargin...
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardElevation="2dp"
card_view:cardBackgroundColor="#color/some_color"
card_view:cardCornerRadius="5dp">
<!-- The left margin decides the width of the border -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginLeft="5dp"
android:background="#fff"
android:orientation="vertical">
<TextView
style="#style/Base.TextAppearance.AppCompat.Headline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" />
<TextView
style="#style/Base.TextAppearance.AppCompat.Body1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Content here" />
</LinearLayout>
</android.support.v7.widget.CardView>
I solved this by putting two CardViews in a RelativeLayout. One with background of the border color and the other one with the image. (or whatever you wish to use)
Note the margin added to top and start for the second CardView. In my case I decided to use a 2dp thick border.
<android.support.v7.widget.CardView
android:id="#+id/user_thumb_rounded_background"
android:layout_width="36dp"
android:layout_height="36dp"
app:cardCornerRadius="18dp"
android:layout_marginEnd="6dp">
<ImageView
android:id="#+id/user_thumb_background"
android:background="#color/colorPrimaryDark"
android:layout_width="36dp"
android:layout_height="36dp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/user_thumb_rounded"
android:layout_width="32dp"
android:layout_height="32dp"
app:cardCornerRadius="16dp"
android:layout_marginTop="2dp"
android:layout_marginStart="2dp"
android:layout_marginEnd="6dp">
<ImageView
android:id="#+id/user_thumb"
android:src="#drawable/default_profile"
android:layout_width="32dp"
android:layout_height="32dp" />
</android.support.v7.widget.CardView>
You can keep <androidx.cardview.widget.CardView
with android:foreground="#drawable/black_border"
black_border :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/white" />
<stroke
android:width="1dp"
android:color="#android:color/black" />
</shape>