In Jetpack Compose Glimmer, the Icon component is a UI element for
rendering single-color icons. Icons intelligently handle tinting and scaling so
that they remain legible and visually consistent with the GlimmerTheme.
Sizes
While icons default to the size provided by LocalIconSize, you can also
use the three icon sizes provided to set an specific size. These sizes are also
used by default for the following contexts:
| Size token | Default usage |
|---|---|
|
For standard list items or small chips. |
|
For standalone icons and title chips. |
|
For high-emphasis components like cards. |
Icon sources
Icons can accept ImageVector, ImageBitmap, or Painter as
their source. When defining your own icons, use ImageVector where possible to
promote sharp rendering at any scale on display glasses.
Color and Tinting
- Automatic tint: The icon resolves its color based on the
LocalContentColorprovided by the parent surfaceLocalContentColorprovided by the parent surface, such as asurfaceorButton. - Manual Tinting: Use the
tintparameter to apply a specific color. - Multicolored Assets: For icons that should not be tinted (like
multicolored brand logos), set
tint = Color.Unspecified. - Generic Images: For photographs or generic images that don't follow icon
sizing and tinting rules, use the standard
androidx.compose.foundation.Imageinstead.
Example: Basic icon within a surface
The following code creates an icon placed inside a circular surface, utilizing the theme's primary color:
@Composable fun IconSampleUsage() { GlimmerLazyColumn( modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, ) { item { IconSizesSample() } item { Icon( FavoriteIcon, "Localized description", Modifier.surface( shape = CircleShape, color = GlimmerTheme.colors.primary, border = null, ) .padding(12.dp), ) } } }
Example: Icons with different sizes
The following code demonstrates the different icon sizes:
@Composable fun IconSizesSample() { val iconSizes = GlimmerTheme.iconSizes Column( verticalArrangement = Arrangement.spacedBy(20.dp), horizontalAlignment = Alignment.CenterHorizontally, ) { Icon(FavoriteIcon, "Localized description", Modifier.size(iconSizes.small)) // medium is also the default size, defining explicitly for clarity Icon(FavoriteIcon, "Localized description", Modifier.size(iconSizes.medium)) Icon(FavoriteIcon, "Localized description", Modifier.size(iconSizes.large)) } }
Key points about the code
- Each icon's size is customized using
GlimmerTheme.iconSizeswith a modifier. For icons, the default value isGlimmerTheme.iconSizes.medium. Use these sizes instead of hard-coding values to maintain consistency across your UI. - Provides a localized
contentDescriptionfor each icon. Always provide these descriptions unless the icon is purely decorative.