Introduction
The concept of the expandable card is widely recognized within the realm of Android applications. Expandable cards allow users to interact with content that can be expanded or collapsed, providing a more organized and user-friendly interface. In this article, we will dive into the concept of expandable cards and explore how to create them using Jetpack Compose.

Here we will make a custom composable with some parameters that will allow us to customize our composable function. we can directly use this function in any project. Here is the function explained in detail-
Step 1. Defining the Composable Function
The expandable card is encapsulated within a composable function named ExpandableCard. This function is annotated @Composable, indicating that it defines a UI component that can be used within Jetpack Compose's compositional structure.
fun ExpandableCard(
title: String,
titleFontSize: TextUnit = MaterialTheme.typography.titleLarge.fontSize,
titleFontWeight: FontWeight = FontWeight.Bold,
description: String,
descriptionFontSize: TextUnit = MaterialTheme.typography.titleSmall.fontSize,
descriptionFontWeight: FontWeight = FontWeight.Normal,
descriptionMaxLines: Int = 4,
shape: Shape = sharpShapes.medium,
padding: Dp = 12.dp
) {
// Card components to be
}
Step 2. Managing Expanded State
The core functionality of the expandable card revolves around its ability to expand and collapse its content. This behavior is managed using a mutable state variable named expandedState. When the card is clicked, this state toggles between true (expanded) and false (collapsed), thereby controlling the visibility of the description text.
var expandedState by remember { mutableStateOf(false) }
Step 3. Animating the Arrow Rotation
The expand/collapse arrow icon's rotation is animated using the animateFloatAsState function. This animation smoothly rotates the arrow from 0 degrees (collapsed state) to 180 degrees (expanded state) and vice versa. The rotationState holds the current rotation angle based on the expandedState.
val rotationState by animateFloatAsState(
targetValue = if (expandedState) 180f else 0f
)
Step 4. Creating the Card Container
The Card composable is utilized to create the visual representation of the expandable card. It provides a styled container with elevation, background, and rounded corners. The Card takes several modifiers, including fillMaxWidth() to stretch across the available width and animateContentSize() to enable smooth content-size animations.


Join the conversation! Your thoughts help the community grow.