Friday, March 6, 2020

android kotlin - MaterialCardView shadow color example

MainActivity.kt

package com.example.jetpack

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/materialCardView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        app:cardBackgroundColor="#54626F"
        app:contentPadding="16dp"
        app:cardElevation="24dp"
        app:cardMaxElevation="36dp"
        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:textColor="#fff"
            android:textAppearance=
                "@style/TextAppearance.MaterialComponents.Headline5"
            />
    </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_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:layout_marginEnd="32dp"
        android:outlineAmbientShadowColor="#E30022"
        android:outlineSpotShadowColor="#E30022"
        app:cardBackgroundColor="#54626F"
        app:cardElevation="24dp"
        app:cardMaxElevation="36dp"
        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:textAppearance=
                "@style/TextAppearance.MaterialComponents.Headline5"
            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_marginStart="32dp"
        android:layout_marginTop="64dp"
        android:layout_marginEnd="32dp"
        app:cardBackgroundColor="#54626F"
        app:cardElevation="24dp"
        app:cardMaxElevation="36dp"
        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:textAppearance=
                "@style/TextAppearance.MaterialComponents.Headline5"
            android:textColor="#fff" />
    </com.google.android.material.card.MaterialCardView>

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples