CardView for Android applications. CardView is a UI component introduced in the Android Support Library to create cards with a consistent appearance. Before using CardView, be sure to include the required dependencies in your app's build.gradle file:
There Are Some Common Attributes for CardView :
id | This ID serves as the control's unique identity. |
layout_width | match_parent -> Text area contains fullscreen wrap_content-> Text area contains only text size |
layout_width | match_parent -> Text area contains fullscreen wrap_content-> Text area contains only text size |
cardCornerRadius | 10 - > For Corner Radius |
cardElevation | 5 -> For Card Shedo |
Step 1:
add build.gradle file
implementation("androidx.cardview:cardview:1.0.0")
plugins {
id("com.android.application")
}
android {
namespace = "com.microappvalley.newtestproject"
compileSdk = 34
defaultConfig {
applicationId = "com.microappvalley.newtestproject"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
implementation("androidx.cardview:cardview:1.0.0") //add for cardview--------------
}
File open res/layout/activity_main.xml file −
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="10dp"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="10dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/profile1" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</RelativeLayout>
Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.