Pro devs. I'll try to change the card view background and stroke color programmatically but all times card.setCardBackgroundColor()
override the stroke color. Statically in XML, it works well. But programmatically it make an effect like this.
val colorWithAlpha = ColorUtils.setAlphaComponent(tagColor, 128)
tagView.card.setStrokeColor(ColorStateList.valueOf(colorWithAlpha))
tagView.card.setCardBackgroundColor(ColorStateList.valueOf(tagColor))
Same for: card.backgroundTintList = ColorStateList.valueOf(tagColor)
XML:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 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:id="@+id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#DB8A61"
app:cardCornerRadius="8dp"
app:cardElevation="0dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="true"
app:strokeColor="#EBD3C7"
app:strokeWidth="4dp">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/tag_text"
style="@style/SmallLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:lines="1"
android:paddingStart="8dp"
android:paddingTop="4dp"
android:paddingEnd="8dp"
android:paddingBottom="4dp"
android:textColor="@color/white"
app:autoSizeMaxTextSize="100sp"
app:autoSizeMinTextSize="5sp"
app:autoSizeStepGranularity="1sp"
app:autoSizeTextType="uniform"
tools:text="Some Text" />
</com.google.android.material.card.MaterialCardView>
Doing that in code:
private fun createAddTagView(tagName: String, tagColor: Int, container: ViewGroup) {
val tagView = LayoutTagBinding.inflate(inflater, container, true)
tagView.tagText.text = tagName
val colorWithAlpha = ColorUtils.setAlphaComponent(tagColor, 128)
tagView.card.setStrokeColor(ColorStateList.valueOf(colorWithAlpha))
tagView.card.setCardBackgroundColor(ColorStateList.valueOf(tagColor))
}
When Removing: tagView.card.setCardBackgroundColor(ColorStateList.valueOf(tagColor))
When there are no Programmatic changes (colors setup by XML
) Looks like expected:
How to make it like in XML
show?