androidx.compose.ui.res

Exceptions

ResourceResolutionException

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

android

Top-level functions summary

Boolean

Load a boolean resource.

android
Color

Load a color resource.

android
Dp

Load a dimension resource.

android
Typeface

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

android
IntArray

Load an array of integer resource.

android
Int

Load an integer resource.

android
ImageBitmap

Load and decode ImageBitmap from the given inputStream.

android
Painter
loadSvgPainter(inputStream: InputStream, density: Density)

Synchronously load an SVG image from some inputStream.

android
ImageVector
loadXmlImageVector(inputSource: InputSource, density: Density)

Synchronously load an xml vector image from some inputSource.

android
Painter

Create a Painter from an Android resource id.

android
Painter

Load a Painter from an resource stored in resources for the application and decode it based on the file extension.

android
String

Load a plurals resource.

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

Load a plurals resource with provided format arguments.

android
Array<String>

Load a string resource.

android
String

Load a string resource.

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

Load a string resource with formatting.

android
inline T
<T : Any?> useResource(resourcePath: String, block: (InputStream) -> T)

Open InputStream from a resource stored in resources for the application, calls the block callback giving it a InputStream and closes stream once the processing is complete.

android

Extension functions summary

ImageBitmap

Load an ImageBitmap from an image resource.

android
ImageBitmap

Load an ImageBitmap from an image resource.

android
ImageVector

Load an ImageVector from a vector resource.

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

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

loadImageBitmap

fun loadImageBitmap(inputStream: InputStream): ImageBitmap

Load and decode ImageBitmap from the given inputStream. inputStream should contain encoded raster image in a format supported by Skia (BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP)

Parameters
inputStream: InputStream

input stream to load an rater image. All bytes will be read from this stream, but stream will not be closed after this method.

Returns
ImageBitmap

the decoded SVG image associated with the resource

loadSvgPainter

fun loadSvgPainter(inputStream: InputStream, density: Density): Painter

Synchronously load an SVG image from some inputStream.

In contrast to svgResource this function isn't Composable

Parameters
inputStream: InputStream

input stream to load an SVG resource. All bytes will be read from this stream, but stream will not be closed after this method.

density: Density

density that will be used to set the intrinsic size of the Painter. If the image will be drawn with the specified size, density will have no effect.

Returns
Painter

the decoded SVG image associated with the resource

loadXmlImageVector

fun loadXmlImageVector(inputSource: InputSource, density: Density): ImageVector

Synchronously load an xml vector image from some inputSource.

XML Vector Image is came from Android world. See: https://developer.android.com/guide/topics/graphics/vector-drawable-resources

On desktop it is fully implemented except there is no resource linking (for example, we can't reference to color defined in another file)

Parameters
inputSource: InputSource

input source to load xml vector image. Will be closed automatically.

density: Density

density that will be used to set the default size of the ImageVector. If the image will be drawn with the specified size, density will have no effect.

Returns
ImageVector

the decoded vector image associated with the image

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.res.painterResource

// 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.draw.drawBehind
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas

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

painterResource

@Composable
fun painterResource(resourcePath: String): Painter

Load a Painter from an resource stored in resources for the application and decode it based on the file extension.

Supported formats:

  • SVG

  • XML vector drawable (see https://developer.android.com/guide/topics/graphics/vector-drawable-resources)

  • raster formats (BMP, GIF, HEIF, ICO, JPEG, PNG, WBMP, WebP)

To load an image from other places (file storage, database, network), use these functions inside LaunchedEffect or remember: loadImageBitmap loadXmlImageVector

Parameters
resourcePath: String

path to the file in the resources folder

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

useResource

inline fun <T : Any?> useResource(resourcePath: String, block: (InputStream) -> T): T

Open InputStream from a resource stored in resources for the application, calls the block callback giving it a InputStream and closes stream once the processing is complete.

Returns
T

object that was returned by block

Throws
kotlin.IllegalArgumentException

if there is no resourcePath in resources

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