Mukesh
Mukesh

Reputation: 164

Setting strokeColor to a CardView programmatically

I'm trying setting strokeColor programmatically but it isn't working. Here's what I've tried

gold_card.strokeColor= Color.GREEN

Looking at official method of strokeColor, it needs a parameter

public void setStrokeColor(@ColorInt int strokeColor) {
    this.cardViewHelper.setStrokeColor(strokeColor);
}

So, upon passing Color int parameter, it says unreachable code. Below method says unreachable code

gold_card.strokeColor(Color.GRAY)

So how to set strokeColor to a CardView the correct way? PS: Yes, I'm using MaterailCardView

Upvotes: 2

Views: 2992

Answers (3)

Sharone Lev
Sharone Lev

Reputation: 841

You need to define gold_card as MaterialCardView, not as CardView.

MaterialCardView gold_card = findViewById(R.id.gold_card)

Upvotes: 3

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364868

You can use the method setStrokeColor:

gold_card.setStrokeColor(ContextCompat.getColor(this,R.color.xxxx));

Also make sure to set the strokeWidth (in the layout with app:strokeWidth or programmatically with the method setStrokeWidth()) because the default value is 0dp

Upvotes: 2

Sachin Soma
Sachin Soma

Reputation: 3822

Change to...

gold_card.strokeColor(R.color.GRAY);

or

gold_card.strokeColor(getResources().getColor(R.color.GRAY));

or

gold_card.strokeColor(Color.parseColor("#C8E6C9")); //your color code

Upvotes: 0

Related Questions