androidx.compose.ui.res

Exceptions

ResourceResolutionException

Throwable that is thrown in situations where a resource failed to load.

Top-level functions summary

Boolean

Load a boolean resource.

Color

Load a color resource.

Dp

Load a dimension resource.

Typeface

This function is deprecated. Prefer to preload fonts using FontFamily.Resolver.

IntArray

Load an array of integer resource.

Int

Load an integer resource.

Painter

Create a Painter from an Android resource id.

String

Load a plurals resource.

String
@Composable
pluralStringResource(id: @PluralsRes Int, count: Int, vararg formatArgs: Any)

Load a plurals resource with provided format arguments.

Array<String>

Load a string resource.

String

Load a string resource.

String
@Composable
stringResource(id: @StringRes Int, vararg formatArgs: Any)

Load a string resource with formatting.

Extension functions summary

ImageBitmap

Load an ImageBitmap from an image resource.

ImageBitmap

Load an ImageBitmap from an image resource.

ImageVector

Load an ImageVector from a vector resource.

ImageVector
ImageVector.Companion.vectorResource(
    theme: Resources.Theme?,
    res: Resources,
    resId: Int
)

Top-level functions

booleanResource

@Composable
fun booleanResource(id: @BoolRes Int): Boolean

Load a boolean resource.

Parameters
id: @BoolRes Int

the resource identifier

Returns
Boolean

the boolean associated with the resource

colorResource

@Composable
fun colorResource(id: @ColorRes Int): Color

Load a color resource.

Parameters
id: @ColorRes Int

the resource identifier

Returns
Color

the color associated with the resource

dimensionResource

@Composable
fun dimensionResource(id: @DimenRes Int): Dp

Load a dimension resource.

Parameters
id: @DimenRes Int

the resource identifier

Returns
Dp

the dimension value associated with the resource

fontResource

@Composable
fun fontResource(fontFamily: FontFamily): Typeface

Synchronously load an font from FontFamily.

Parameters
fontFamily: FontFamily

the fontFamily

Returns
Typeface

the decoded image data associated with the resource

integerArrayResource

@Composable
fun integerArrayResource(id: @ArrayRes Int): IntArray

Load an array of integer resource.

Parameters
id: @ArrayRes Int

the resource identifier

Returns
IntArray

the integer array associated with the resource

integerResource

@Composable
fun integerResource(id: @IntegerRes Int): Int

Load an integer resource.

Parameters
id: @IntegerRes Int

the resource identifier

Returns
Int

the integer associated with the resource

painterResource

@Composable
fun painterResource(id: @DrawableRes Int): Painter

Create a Painter from an Android resource id. This can load either an instance of BitmapPainter or VectorPainter for ImageBitmap based assets or vector based assets respectively. The resources with the given id must point to either fully rasterized images (ex. PNG or JPG files) or VectorDrawable xml assets. API based xml Drawables are not supported here.

Example:

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.paint
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp

// Sample showing how to render a Painter based on a different resource (vector vs png)
// Here a Vector asset is used in the portrait orientation, however, a png is used instead
// in the landscape orientation based on the res/drawable and res/drawable-land-hdpi folders
Image(
    painterResource(R.drawable.ic_vector_or_png),
    contentDescription = null,
    modifier = Modifier.requiredSize(50.dp)
)

Alternative Drawable implementations can be used with compose by calling drawIntoCanvas and drawing with the Android framework canvas provided through nativeCanvas

Example:

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp

val drawable = LocalContext.current.getDrawable(R.drawable.sample_drawable)
Box(
    modifier =
        Modifier.requiredSize(100.dp).drawBehind {
            drawIntoCanvas { canvas ->
                drawable?.let {
                    it.setBounds(0, 0, size.width.roundToInt(), size.height.roundToInt())
                    it.draw(canvas.nativeCanvas)
                }
            }
        }
)
Parameters
id: @DrawableRes Int

Resources object to query the image file from

Returns
Painter

Painter used for drawing the loaded resource

pluralStringResource

@Composable
fun pluralStringResource(id: @PluralsRes Int, count: Int): String

Load a plurals resource.

Parameters
id: @PluralsRes Int

the resource identifier

count: Int

the count

Returns
String

the pluralized string data associated with the resource

pluralStringResource

@Composable
fun pluralStringResource(id: @PluralsRes Int, count: Int, vararg formatArgs: Any): String

Load a plurals resource with provided format arguments.

Parameters
id: @PluralsRes Int

the resource identifier

count: Int

the count

vararg formatArgs: Any

arguments used in the format string

Returns
String

the pluralized string data associated with the resource

stringArrayResource

@Composable
fun stringArrayResource(id: @ArrayRes Int): Array<String>

Load a string resource.

Parameters
id: @ArrayRes Int

the resource identifier

Returns
Array<String>

the string data associated with the resource

stringResource

@Composable
fun stringResource(id: @StringRes Int): String

Load a string resource.

Parameters
id: @StringRes Int

the resource identifier

Returns
String

the string data associated with the resource

stringResource

@Composable
fun stringResource(id: @StringRes Int, vararg formatArgs: Any): String

Load a string resource with formatting.

Parameters
id: @StringRes Int

the resource identifier

vararg formatArgs: Any

the format arguments

Returns
String

the string data associated with the resource

Extension functions

imageResource

@Composable
fun ImageBitmap.Companion.imageResource(id: @DrawableRes Int): ImageBitmap

Load an ImageBitmap from an image resource.

This function is intended to be used for when low-level ImageBitmap-specific functionality is required. For simply displaying onscreen, the vector/bitmap-agnostic painterResource is recommended instead.

Parameters
id: @DrawableRes Int

the resource identifier

Returns
ImageBitmap

the decoded image data associated with the resource

imageResource

fun ImageBitmap.Companion.imageResource(res: Resources, id: @DrawableRes Int): ImageBitmap

Load an ImageBitmap from an image resource.

This function is intended to be used for when low-level ImageBitmap-specific functionality is required. For simply displaying onscreen, the vector/bitmap-agnostic painterResource is recommended instead.

Returns
ImageBitmap

Loaded image file represented as an ImageBitmap

vectorResource

@Composable
fun ImageVector.Companion.vectorResource(id: @DrawableRes Int): ImageVector

Load an ImageVector from a vector resource.

This function is intended to be used for when low-level ImageVector-specific functionality is required. For simply displaying onscreen, the vector/bitmap-agnostic painterResource is recommended instead.

Parameters
id: @DrawableRes Int

the resource identifier

Returns
ImageVector

the vector data associated with the resource

vectorResource

fun ImageVector.Companion.vectorResource(
    theme: Resources.Theme? = null,
    res: Resources,
    resId: Int
): ImageVector