Anas Hafidi
Anas Hafidi

Reputation: 425

Change Raduis CardView Background Color

I have a CardView with radius and stroke, but when I change the CardView Background programmatically, the radius and stroke are lost, I want the new color to stay inside the stroke.

This is my cardview xml

<com.google.android.material.card.MaterialCardView
      android:id="@+id/answerOneTransport"
      android:layout_width="148dp"
      android:layout_height="60dp"
      android:layout_marginStart="32dp"
      android:layout_marginTop="32dp"
      app:cardBackgroundColor="#F7F7F7"
      app:cardCornerRadius="16dp"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/quizzTransportViewPager"
      app:strokeColor="#9890FF"
      app:strokeWidth="1dp" >
      <TextView
          android:id="@+id/textAnswwerOneTransport"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Answer One"
          android:layout_gravity="center"
          android:fontFamily="@font/nunito"
          android:textSize="20sp"
          />
  </com.google.android.material.card.MaterialCardView> 

Here is where I change color:

answerOneTransport.setOnClickListener {
    answerOneTransport.setBackgroundColor(Color.GREEN)
}

Upvotes: 2

Views: 327

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364674

To change the background color of the CardView you have to use the method:

cardView.setCardBackgroundColor(ContextCompat.getColor(this,R.color.xxx));

enter image description here

Upvotes: 3

Kamal Nayan
Kamal Nayan

Reputation: 1718

You need to use android:backgroundTint

Upvotes: 0

Related Questions