Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setStrokeColor for com.google.android.material.card.MaterialCardView programmatically

I tried the code below in my adapter class, but it is not working.

myViewHolder.cardview1.setStrokeColor(ContextCompat.getColor(context, R.color.selected_color));
like image 1000
Kannan Avatar asked Aug 29 '19 11:08

Kannan


People also ask

What is MaterialCardView in android?

MaterialCardView is a customizable component based on CardView from the Android Support Library. MaterialCardView provides all of the features of CardView , but adds attributes for customizing the stroke and uses an updated Material style by default.

Is CardView deprecated?

This method is deprecated.


1 Answers

I had the same problem, to solve it just add the invalidate() method:

myViewHolder.cardview1.setStrokeColor(ContextCompat.getColor(context, R.color.selected_color));
myViewHolder.cardview1.invalidate();

This updates the view.

like image 134
Pillocron Avatar answered Sep 17 '22 19:09

Pillocron