Jetpack Compose can access the resources defined in your Android project. This document explains some of the APIs Compose offers to do so.
Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. If you're not familiar with resources in Android, check out the App resources overview guide.
Strings
The most common type of resource are your Strings. Use the stringResource
API
to retrieve a string statically defined in your XML resources.
// In the res/values/strings.xml file
// <string name="compose">Jetpack Compose</string>
// In your Compose code
Text(
text = stringResource(R.string.compose)
)
stringResource
also works with positional formatting.
// In the res/values/strings.xml file
// <string name="congratulate">Happy %1$s %2$d</string>
// In your Compose code
Text(
text = stringResource(R.string.congratulate, "New Year", 2021)
)
String plurals
Compose doesn't offer a direct method to retrieve String
plurals yet. However,
you can use the traditional approach with the
getQuantityString
method of the Resources
class. To
access Resources
from the current
Context
, use the
LocalContext
composition local. Read more about Composition locals in the
Interoperability documentation.
// In the res/strings.xml file
// <plurals name="runtime_format">
// <item quantity="one">%1$d minute</item>
// <item quantity="other">%1$d minutes</item>
// </plurals>
// In your Compose code
val resources = LocalContext.current.resources
Text(
text = resources.getQuantityString(
R.plurals.runtime_format, quantity, quantity
)
)
Dimensions
Similarly, use the dimensionResource
API to get dimensions from a resource XML
file.
// In the res/values/dimens.xml file
// <dimen name="padding_small">8dp</dimen>
// In your Compose code
val smallPadding = dimensionResource(R.dimen.padding_small)
Text(
text = "...",
modifier = Modifier.padding(smallPadding)
)
Colors
If you're adopting Compose incrementally in your app, use the colorResource
API to get colors from a resource XML file.
// In the res/colors.xml file
// <color name="colorGrey">#757575</color>
// In your Compose code
Divider(color = colorResource(R.color.colorGrey))
colorResource
works as expected with static colors, but it flattens color
state list resources.
Vector assets and image resources
Use the painterResource
API to load either vector drawables or rasterized
asset formats like PNGs. You don't need to know the type of the drawable, simply
use painterResource
in Image
composables or paint
modifiers.
// Files in res/drawable folders. For example:
// - res/drawable-nodpi/ic_logo.xml
// - res/drawable-xxhdpi/ic_logo.png
// In your Compose code
Icon(
painter = painterResource(id = R.drawable.ic_logo),
contentDescription = null // decorative element
)
painterResource
decodes and parses the content of the resource on the main
thread.
Animated Vector Drawables
Use the animatedVectorResource
API to load an animated vector drawable XML.
The method returns an AnimatedImageVector
instance. In order to display the
animated image, use the painterFor
method to create a Painter
that can be
used in Image
and Icon
composables. The boolean atEnd
parameter of the
painterFor
method indicates whether the image should be drawn at the end of
all the animations. If used with a mutable state, changes to this value trigger
the corresponding animation.
// Files in res/drawable folders. For example:
// - res/drawable/animated_vector.xml
// In your Compose code
val image = animatedVectorResource(id = R.drawable.animated_vector)
val atEnd by remember { mutableStateOf(false) }
Icon(
painter = image.painterFor(atEnd = atEnd),
contentDescription = null // decorative element
)
Icons
Jetpack Compose comes with the Icons
object that is the entry point for using
Material Icons in
Compose. There are five distinct icon themes:
Filled,
Outlined,
Rounded,
TwoTone, and
Sharp. Each
theme contains the same icons, but with a distinct visual style. You should
typically choose one theme and use it across your application for consistency.
To draw an icon, you can use the
Icon
composable which applies tint and provides layout size matching the icon.
import androidx.compose.material.Icon
Icon(Icons.Rounded.Menu, contentDescription = "Localized description")
Some of the most commonly used icons are available as part of the
androidx.compose.material
dependency. To use any of the other Material icons,
add the material-icons-extended
dependency to the build.gradle
file.
dependencies {
...
implementation "androidx.compose.material:material-icons-extended:$compose_version"
}
Fonts
To use fonts in Compose, download and bundle the font files directly in your
APKs by placing them in the res/font
folder.
Load each font using the
Font
API and create a
FontFamily
with
them that you can use in
TextStyle
instances to
create your own
Typography
. The
following is code taken from the
Crane
compose sample and its
Typography.kt
file.
// Define and load the fonts of the app
private val light = Font(R.font.raleway_light, FontWeight.W300)
private val regular = Font(R.font.raleway_regular, FontWeight.W400)
private val medium = Font(R.font.raleway_medium, FontWeight.W500)
private val semibold = Font(R.font.raleway_semibold, FontWeight.W600)
// Create a font family to use in TextStyles
private val craneFontFamily = FontFamily(light, regular, medium, semibold)
// Use the font family to define a custom typography
val craneTypography = Typography(
defaultFontFamily = craneFontFamily,
/* ... */
)
// Pass the typography to a MaterialTheme that will create a theme using
// that typography in the part of the UI hierarchy where this theme is used
@Composable
fun CraneTheme(content: @Composable () -> Unit) {
MaterialTheme(typography = craneTypography) {
content()
}
}
Learn more about typography in the Theming in Compose documentation.