MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import com.google.android.material.card.MaterialCardView
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val materialCardView3 = findViewById<MaterialCardView>(
R.id.materialCardView3)
// programmatically set material card view shadow color
materialCardView3.apply {
// call require minimum api level 28
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
outlineAmbientShadowColor = Color.YELLOW
outlineSpotShadowColor = Color.YELLOW
}
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:id="@+id/rootLayout"
android:padding="24dp">
<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#54626F"
app:cardElevation="12dp"
app:cardMaxElevation="16dp"
app:contentPadding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textview.MaterialTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Regular shadow color"
android:fontFamily="sans-serif"
android:textSize="24sp"
android:textColor="#fff" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:outlineAmbientShadowColor="#E30022"
android:outlineSpotShadowColor="#E30022"
app:cardBackgroundColor="#54626F"
app:cardElevation="12dp"
app:cardMaxElevation="16dp"
app:contentPadding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialCardView1">
<com.google.android.material.textview.MaterialTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Red shadow color (Min API 28)"
android:fontFamily="sans-serif"
android:textSize="24sp"
android:textColor="#fff" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="@+id/materialCardView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
app:cardBackgroundColor="#54626F"
app:cardElevation="12dp"
app:cardMaxElevation="16dp"
app:contentPadding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialCardView2">
<com.google.android.material.textview.MaterialTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yellow shadow color programmatically (API 28 Min)"
android:fontFamily="sans-serif"
android:textSize="24sp"
android:textColor="#fff" />
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies[add]
// Material components
implementation 'com.google.android.material:material:1.6.1'