Custom curvy shape for a CardView in android - android

I've just started with android and, in order to learn something, i'm simply trying to recreate some basic concept that i found on the web. My biggest question so far is which is the best method to draw a custom curvy shape in android. I know that similar question have been asked multiple times here, but i'm unable to find the solution to my problem.
I do not understand how to replicate a card like this. My only idea so far is to create curvy line in illustrator, save into svg and import in android as vector assets. At this point i was simply thinking to create a white rectangle and overlay the vectori assets. I absolutely don't think this is teh best way to do it but so far i don't know another way.
Thanks and sorry my english

either user CardView or create your custom view.
CardView
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="#dimen/scale_7dp"
app:cardElevation="#dimen/scale_5dp"
app:cardUseCompatPadding="true">
</android.support.v7.widget.CardView>
Custom View
create a shape in your drawable.xml folder
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/dull_white" />
<corners
android:bottomLeftRadius="#dimen/scale_5dp"
android:bottomRightRadius="#dimen/scale_5dp"
android:topLeftRadius="#dimen/scale_5dp"
android:topRightRadius="#dimen/scale_5dp" />
</shape>
Hope this helps you.

The best implementation would be to use CardView in your layout.
include
compile 'com.android.support:cardview-v7:21.0.0-rc1'
or any other version which is compatible with your existing support library.
after that, use this in your layout xml file
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#color/grey_300"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">
place all the other views inside this cardview and you will get a curvy edge.

Add this dependency in app, this lib will give a rounded corner Image and other for cardview.
implementation 'com.makeramen:roundedimageview:2.3.0'
implementation 'com.android.support:cardview-v7:28.0.0-rc01'
In xml parent CardView and its Child RoundedImageView have same corner radius. I hope you know, how cardview works. Where "YOUR_DRAWABLE_HERE" is written please change it with your drawable
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="10dp"
app:cardElevation="0dp"
app:cardBackgroundColor="#00ffffff"
android:layout_width="match_parent"
android:layout_height="100dp">
<com.makeramen.roundedimageview.RoundedImageView
android:id="#+id/iv_look"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="YOUR_DRAWABLE_HERE"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:riv_corner_radius="10dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="match_parent">
<ImageView
android:layout_margin="20dp"
android:layout_width="100dp"
android:src="#android:drawable/btn_default"
android:layout_height="50dp" />
<TextView
android:text="YOUR TEXT"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>

Related

CardView with rounded corners [duplicate]

Is there a way to make CardView only have corner radius at the top?
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10dp"
>
Unless you try to extend the Android CardView class, you cannot customize that attribute from XML.
Nonetheless, there is a way of obtaining that effect.
Place a CardView inside another CardView and apply a transparent background to your outer CardView and remove its corner radius ("cornerRadios = 0dp"). Your inner CardView will have a cornerRadius value of 3dp, for example. Then apply a marginTop to your inner CardView, so its bottom bounds will be cut by the outer CardView. This way, the bottom corner radius of your inner CardView will be hidden.
The XML code is the following:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_outer"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
card_view:cardBackgroundColor="#android:color/transparent"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="3dp" >
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_inner"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="3dp"
card_view:cardBackgroundColor="#color/green"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="0dp" >
</android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>
And the visual effect is the following:
Always put your content in your Inner CardView. Your outer CardView serves only the purpose of "hiding" the bottom Rounded Corners of the inner CardView.
You can use the standard MaterialCard included in the official Material Components library.
Use in your layout:
<com.google.android.material.card.MaterialCardView
style="#style/MyCardView"
...>
In your style use the shapeAppearanceOverlay attribute to customize the shape (the default corner radius is 4dp)
<style name="MyCardView" parent="#style/Widget.MaterialComponents.CardView">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay.MaterialCardView.Cut</item>
</style>
<style name="ShapeAppearanceOverlay.MaterialCardView.Cut" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSizeTopRight">8dp</item>
<item name="cornerSizeTopLeft">8dp</item>
<item name="cornerSizeBottomRight">0dp</item>
<item name="cornerSizeBottomLeft">0dp</item>
</style>
You can also use:
<com.google.android.material.card.MaterialCardView
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.MaterialCardView.Cut"
...>
It is the result:
With Jetpack compose you can use the shape parameter in the Card.
Something like:
Card(
shape = RoundedCornerShape(
topStart = 8.dp,
topEnd = 8.dp,
bottomEnd = 0.dp,
bottomStart = 0.dp,
)
){
Text("Content Card")
}
dependencies: compile 'com.android.support:cardview-v7:23.1.1'
<android.support.v7.widget.CardView
android:layout_width="80dp"
android:layout_height="80dp"
android:elevation="12dp"
android:id="#+id/view2"
app:cardCornerRadius="40dp"
android:layout_centerHorizontal="true"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9">
<ImageView
android:layout_height="80dp"
android:layout_width="match_parent"
android:id="#+id/imageView1"
android:src="#drawable/Your_image"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
</android.support.v7.widget.CardView>
There is an example how to achieve it, when the card is at the very bottom of the screen. If someone has this kind of problem just do something like that:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-5dp"
card_view:cardCornerRadius="4dp">
<SomeView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp">
</SomeView>
</android.support.v7.widget.CardView>
Card View has a negative bottom margin. The view inside a Card View has the same, but positive bottom margin. This way rounded parts are hidden below the screen, but everything looks exactly the same, because the inner view has a counter margin.
You can use this drawable xml and set as background to cardview :
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="1dp"
android:color="#ff000000"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
I wrote a drawable lib to custom round corner position, it looks like this:
You can get this lib at here:
https://github.com/mthli/Slice
As #Gabriele Mariotti at feedback#2 suggested. Thanks so much #Gabriele bcz your feedback helped me on this case but my account can't vote for you at this time, so sorry :( (I also need helping like this topic creater)
I recommend to you shoud use MaterialCardView component (not CardView). Exactly is MaterialCardView.
So, this is the code in XML layout:
<com.google.android.material.card.MaterialCardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="260dp"
android:layout_marginTop="0dp"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.CardView" >
<ImageButton
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_arrow"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp"
android:background="#android:color/transparent"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imagePoke"
android:layout_width="220dp"
android:layout_height="220dp"
android:layout_gravity="center"
android:scaleType="fitXY"
tools:ignore="ContentDescription" />
</com.google.android.material.card.MaterialCardView>
And below is the code in style/ShapeAppearanceOverlay atribute:
<style name="ShapeAppearanceOverlay.CardView" parent="">
<item name="cornerFamilyBottomLeft">rounded</item>
<item name="cornerFamilyBottomRight">rounded</item>
<item name="cornerSizeBottomLeft">30%</item>
<item name="cornerSizeBottomRight">30%</item>
</style>
Output:
You need to do 2 things :
1) Call setPreventCornerOverlap(false) on your CardView.
2) Put rounded Imageview inside CardView
About rounding your imageview, I had the same problem so I made a library that you can set different radii on each corner. Finally I got the result what I wanted like below.
https://github.com/pungrue26/SelectableRoundedImageView
NOTE: This here is a workaround if you want to achieve rounded corners at the bottom only and regular corners at the top. This will not work if you want to have different radius for all four corners of the cardview. You will have to use material cardview for it or use some third party library.
Here's what seemed to work for me:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#F9F9F9">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/profile_bg"/>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cvProfileHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="32dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="280dp"
android:orientation="vertical"
android:background="#drawable/profile_bg"
android:id="#+id/llProfileHeader"
android:gravity="center_horizontal">
<!--Enter your code here-->
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
There's two cardview's in all. The second cardview is the one that will have rounded corners (on all sides as usual) and will hold all other subviews under it. The first cardview above it is also at the same level (of elevation), and has the same background but is only about half the height of the second cardview and has no rounded corners (just the usual sharp corners). This way I was able to achieve partially rounded corners on the bottom and normal corners on the top. But for all four sides, you may have to use the material cardview.
You could do the reverse of this to get rounded corners at the top and regular ones at the bottom, i.e. let the first cardview have rounded corners and the second cardview have regular corners.
My custom way to implemented with a lib
//Rounded card
add this implementation on build.gradle
implementation 'com.github.captain-miao:optroundcardview:1.0.0'
in XML:
<com.github.captain_miao.optroundcardview.OptRoundCardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
app:optRoundCardCornerRadius="40dp"
app:optRoundCardLeftBottomCorner="false"
app:optRoundCardRightBottomCorner="false"
app:optRoundCardBackgroundColor="#E2EAF8">
The simple way to make rounded corners is by using the CardCornerRadius attribute
app:cardCornerRadius="value"
simply write this in your XML code
<androidx.cardview.widget.CardView
android:id="#+id/cv_info"
...
app:cardCornerRadius="5dp"
...>
and it will work perfectly
image of the cardview
the highest value you'll put in the rounded it will be.
but when we would like to make rounded corners only in certain spots like left bottom, right bottom and so on we will run into a problem, because the CardCornerRadius attribute does it to each side.
I managed to find a solution, not a perfect one, and sometimes this solution is irrelevant for example when you want to do it in the middle of the activity, it will work only if you want this to card view who's in the corners of the activity.
something like that rounded corner image cardView
so what I'm trying to achieve is to make the card view to overflow to activity overflowed cardview
to do this you may set the width of the card view higher than the width of the phone or a better way is to set the width to match the parent and then set the margin to minus.
The XML code will be something like that
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="-16dp"
android:layout_marginLeft="-16dp"
android:layout_marginTop="-26dp"
android:layout_marginEnd="-16dp"
android:layout_marginRight="-16dp"
and then set the app:cardCornerRadius to something high
app:cardCornerRadius="200dp"
and you should get the final result final solution
you can also play with this and make some cool things with that!
I hope it will help someone in the future, I know it's not the most elegant solution or good solution at all but it should do the job, I tried some other things but it seems to be more complicated.
you can use the library:OptionRoundCardview
Easiest way to achieve it in Android Studio is explained below:
Step 1:
Write below line in dependencies in build.gradle:
compile 'com.android.support:cardview-v7:+'
Step 2:
Copy below code in your xml file for integrating the CardView.
For cardCornerRadius to work, please be sure to include below line in parent layout:
xmlns:card_view="http://schemas.android.com/apk/res-auto"
And remember to use card_view as namespace for using cardCornerRadius property.
For example : card_view:cardCornerRadius="4dp"
XML Code:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_outer"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
card_view:cardBackgroundColor="#android:color/transparent"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="3dp" >
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_inner"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="3dp"
card_view:cardBackgroundColor="#color/green"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="0dp" >
</android.support.v7.widget.CardView>
</android.support.v7.widget.CardView>
You can put your ImageView inside a CardView, and add those properties to the ImageView:
android:cropToPadding="true"
android:paddingBottom="15dp"
That way you will get rid of the rounded bottom corner, but keep in mind that this comes in the price of having a small portion of your image cut off.
You will find many ninja techniques on the internet but the best way to do this is to create a "Drawable Resource file" and design your own background and give a radius to every side or only one side.
So, these are the following steps:
Right-click on the Drawable file.
Then Click New and then click drawable resource file.
Then a page will popup you only need to set the name of your file.
Change the Selecter to Shape.
And add this code.
The final step is to set this newly created drawable file as the
background.
An easy way to achieve this would be:
1.Make a custom background resource (like a rectangle shape) with rounded corners.
2.set this custom background using the command -
cardView = view.findViewById(R.id.card_view2);
cardView.setBackgroundResource(R.drawable.card_view_bg);
this worked for me.
The XML layout I made with top left and bottom right radius.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white" />
<corners android:topLeftRadius="18dp" android:bottomRightRadius="18dp" />
</shape>
In your case, you need to change only topLeftRadius as well as topRightRadius.
If you have a layout that overlaps with the corners of the card view and has a different color maybe, then you might need a different background resource file for the layout and in the xml set this background resource to your layout.
I tried and tested the above method. Hope this helps you.
If you're setting the card background programmatically, make use you use cardView.setCardBackgroundColor() and not cardView.setBackgroundColor() and make sure use using app:cardPreventCornerOverlap="true" on the cardView.xml. That fixed it for me.
Btw, the above code (in quotations) is in Kotlin and not Java. Use the java equivalent if you're using Java.

What can I do to shrink the space between my CardViews within a RecyclerView?

So I'm learning app development for Android, and I'm a bit stuck. I'm trying to make a RecyclerView with CardViews, but there is just too much space between CardViews. This is what it looks like.
I'm going for more of the look that the Google app has with the feed.
This is more like what I'm shooting for.
Anyways, I've searched and searched on this website for a solution to my problem and nothing seems to be working for me. I'm hoping that someone can give me something that will actually work.
Here's the xml for my cardview:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="10dp">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/note_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_below="#+id/event_time"
android:textSize="24sp" />
<TextView
android:id="#+id/note_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/note_title" />
</RelativeLayout>
</android.support.v7.widget.CardView>
The top and bottom spaces is caused by your padding
android:paddingBottom="10dp", remove this or reduce it
Decrease the PadingBotom of the root constraintLayout
you could put 3 or 4 dp instead of 10dp.
android:paddingBottom="10dp"
Also I do not know if the copy of your code was not good ... the constraintLayout must close at the end:
after this:
</android.support.v7.widget.CardView>
you must have this:
</android.support.constraint.ConstraintLayout>
From the documentation for CardView:
Before Lollipop, CardView adds padding to its content and draws shadows to that area. ... If you want CardView to add inner padding on platforms Lollipop and after as well, you can call setUseCompatPadding(boolean) and pass true.
In your layout, you have this attribute on your CardView tag:
app:cardUseCompatPadding="true"
This does the same thing as the method in the quote above.
As a result, even if you had zero margin on all sides of your cards, users would still see space in between them because of the inner padding behavior. If you want really tight spacing, you'll have to remove this attribute (though of course you'll still get the larger spacing pre-Lollipop).

Achieving content placeholders as progress indicators in Android

My question is quite similar to this question. I want to achieve progress indicators that use this style:
That is replacing content like text and images with grey rectangles that animate like here.
I have seen this quite a lot in the past, e.g. Facebook. I am pretty sure that the YouTube app used to have this style.
Questions devided
The obvious first question is on how to this on Android and especially if there is an idiomatic, standard or simple way of achieving this and the second one is closely connected to this because here is my
Problem
How would I ever achieve converting a TextView into such a loading rectangle. I could image replacing my whole view structure with this, but I also do not know where to start with the animation.
Take a look at this library - ShimmerLayout
ShimmerLayout can be used to add shimmer effect (like the one used at Facebook or at LinkedIn) to your Android application.
Also, if you curious about how to implement animation, take a look at the library sources.
You need to create the skeleton layout and inflate it on the whole screen. Then you can use different libraries to add the shimmer effect.
drawable/skeleton.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<solid android:color="#color/skeleton"/>
<corners android:radius="4dp"/>
</shape>
layout/skeleton_row_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="#dimen/row_layout_height">
<View
android:id="#+id/icon"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_margin="15dp"
android:layout_gravity="center_vertical"
android:background="#drawable/skeleton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<View
android:id="#+id/topText"
android:layout_width="200dp"
app:layout_constraintTop_toTopOf="#id/icon"
app:layout_constraintStart_toEndOf="#id/icon"
android:layout_height="15dp"
android:layout_marginLeft="16dp"
android:background="#drawable/skeleton"/>
<View
android:id="#+id/bottomText"
android:layout_width="250dp"
android:layout_height="15dp"
app:layout_constraintTop_toBottomOf="#id/topText"
android:layout_marginTop="10dp"
app:layout_constraintStart_toEndOf="#id/icon"
android:layout_marginLeft="16dp"
android:background="#drawable/skeleton"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#drawable/skeleton"
app:layout_constraintTop_toBottomOf="#id/icon"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</android.support.constraint.ConstraintLayout>
Check out this tutorial I wrote to see how to use it: https://medium.com/#sha17187/upgrade-progress-loading-with-a-skeleton-and-shimmer-effect-in-android-863ea4ff5b0b

How to make a rounded surfaceview

I'm trying to make a rounded shaped surfaceview. I've searched a lot but i couldn't find a good solution. what i'm doing right now is that, I've put the SurfaceView into a FrameLayout, then another View on top of it, either with a PNG mask, or a shape xml drawable. here it is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#000"
android:orientation="vertical"
android:visibility="visible" >
<FrameLayout
android:id="#+id/videorecordview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight=".2" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/rounded"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
But this is not a good solution and it is also not working perfectly. I want to customize surfaceview to a rounded shape. any help would be much appreciated. Thank you :)
A little hack. Put your surface view inside card view.
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_normal"
app:cardCornerRadius="10dp"
app:cardPreventCornerOverlap="false">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/margin_normal" />
</android.support.v7.widget.CardView>
Don't forget to add this to your gradle file to use CardView
compile 'com.android.support:cardview-v7:25.0.1'
Also this two line inside card view
app:cardCornerRadius="10dp"
app:cardPreventCornerOverlap="false"
Cheers happy coding
You can't change the shape of the SurfaceView's Surface.
A SurfaceView has two parts, the Surface and the View. The View part works like any other View. By default, it just acts as a transparent "hole", creating a window in the layout. All Views are rendered by the app onto a single layer.
The Surface part is a separate layer that sits behind the View layer (unless you explicitly change the Surface's Z order), so you only see it where it "shows through" transparent areas of the View layer. You can draw on the View layer to mask portions of the Surface, but you can't change the shape of the Surface layer itself. Layers are rectangular.
In many situations a TextureView can be used in place of a SurfaceView. TextureView offers greater flexibility because it's rendered by the app onto the View layer, but can be less efficient than SurfaceView.
More information can be found in the Android graphics architecture doc.
To achieve what you ask, it is very easy to do it.
You only need to use the next XML:
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="12dp">
<SurfaceView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</androidx.cardview.widget.CardView>
And to use this CardView, add this dependency on your build.gradle app:
implementation 'androidx.cardview:cardview:1.0.0'
try this
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<gradient
android:startColor="#FFFF0000"
android:endColor="#80FF00FF"
android:angle="270"/>
</shape>
<SurfaceView
android:background="#drawable/circle"
android:id="#+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

Cardview - white border around card

I am using a cardview as the root of a custom view I am writing. I using the v7 support library. My XML looks like 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:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="6dp"
card_view:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- some other views -->
</LinearLayout>
</android.support.v7.widget.CardView>
My problem is that I am getting a white border around my card view. It looks like it is there to indicate elevation as it is thicker on the right side. I've tried adjusting cardElevation and MaxCardElevation in my XML like so :
card_view:cardElevation="0dp"
and in code in my custom view that extends CardView and uses this layout:
setCardElevation(0);
setMaxCardElevation(0);
But the white border persists. I'm not sure how to get rid of it. If anyone had any input into why this is happening or suggestions on how I can remove the white border it would be appreciated. Thanks much.
I know it's a bit late, but for anyone having a similar problem:
I had the same issue: A white border was shown on pre-lollipop devices.
I solved it setting the cardPreventCornerOverlap to false on your XML.
Like this:
<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:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="6dp"
card_view:cardPreventCornerOverlap="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- some other views -->
</LinearLayout>
</android.support.v7.widget.CardView>
Support CardView doesn't support content clipping, because it's expensive on older devices. It's possible to clip content using Canvas.saveLayer/restoreLayer and PorterDuff modes. This is how Carbon implements rounded corners with correct content clipping. See the image:
I might be late in the game, but I had the same issue. Just wanted to share a simple solution!
My custom view extended a CardView and I had a margin applied to the root element (i.e. CardView) in the XML just like in the original question. This ended up giving me the extra white border like this:
Solution was to move the margin from root of the Custom View XML to the declaration of your custom view (check comments in the snippet)
Code snippet:
<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/cvSettings"
style="#style/DefaultCardViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin" <!-- Remove this -->
app:cardElevation="#dimen/default_card_elevation">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
.
.
.
</android.support.v7.widget.CardView>
Ended up just moving over the margin to my custom view declaration which got rid of the extra white border:
<com.example.android.custom.MyCustomView
android:id="#+id/custom_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/default_margin" <!-- Move it here-->
cv:pt_widgetColor="#color/colorAccent" />
After the change, much cleaner :) :
use cardBackgroundColor="color" in xml card_view
sample code :
<android.support.v7.widget.CardView
android:id="#+id/card_view_khaterat"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
app:cardBackgroundColor="#f56f6c"/>

Categories

Resources