Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change card_view:cardCornerRadius programmatically

In my project, I am using RecyclerView in my listing using CardView. In my listing I have to set the corner radius of CardView dynamically based on device.

Is there any way to set cardview corner radius value dynamically?

Thanks.

like image 430
Faisal Ahsan Avatar asked Jul 04 '16 10:07

Faisal Ahsan


People also ask

How do I customize my card view?

Customized CardView First, add a CardView dependency to the application-level build. gradle file. Then create a drawable background for the cards. For that, create a new drawable resource file inside the drawable folder.

How do you add rounded corners in CardView?

CardView android:id="@+id/cv_info" ... app:cardCornerRadius="5dp" ...> the highest value you'll put in the rounded it will be. you can also play with this and make some cool things with that!


1 Answers

Use CardView.setRadius(float), see androidx.cardview.

(It is worth noting that this will have no effect if you also invoke setBackgroundColor, make sure to use setCardBackgroundColor instead).

You must specify a pixel size, rather than dp value, e.g. for corner radius of 4dp you can invoke, in Kotlin:

radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, context.resources.displayMetrics)  
like image 193
natario Avatar answered Sep 24 '22 22:09

natario