MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.util.TypedValue
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 context
val context = this
// get the widgets reference from XML layout
val materialCardView2 = findViewById<MaterialCardView>(
R.id.materialCardView2)
// programmatically set card corner radius
materialCardView2.apply {
// card corner radius 12 dp
// make it rounded corners card view
radius = 12.toDp(context).toFloat()
}
}
}
// extension method to convert values to dp
fun Int.toDp(context: Context):Int = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
this.toFloat(),
context.resources.displayMetrics
).toInt()
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="#8FBC8F"
app:cardCornerRadius="12dp"
app:cardElevation="8dp"
app:contentPadding="32dp"
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:fontFamily="sans-serif"
android:text="Card Corner Radius In XML"
android:textSize="26sp" />
</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="32dp"
app:cardBackgroundColor="#DE5D83"
app:cardElevation="8dp"
app:contentPadding="32dp"
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:fontFamily="sans-serif"
android:text="Material Card Corner Radius Programmatically."
android:textSize="26sp" />
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies
// Material components
implementation 'com.google.android.material:material:1.6.1'

- kotlin ktx - AbsoluteSizeSpan RelativeSizeSpan
- kotlin ktx - AlignmentSpan
- kotlin syntax - Get string last index
- kotlin syntax - Compare two strings
- kotlin - ConstraintLayout center in parent programmatically
- kotlin - Spinner text size programmatically
- kotlin - MaterialButtonToggleGroup
- kotlin - MaterialCardView shadow color
- kotlin - MaterialCardView checked unchecked
- kotlin - MaterialCardView border color width radius