Google is committed to advancing racial equity for Black communities. See how.

androidx.compose.ui.res

Classes

DeferredResource

A class used for the result of the asynchronous resource loading.

FailedResource

A class represents the alternative resource due to failed to load the requested resource.

LoadedResource

A class represents the loaded resource.

PendingResource

A class represents the alternative resource due to background loading.

Resource

The base resource class for background resource loading.

Top-level functions summary

Boolean
booleanResource(@BoolRes id: Int)

Load a boolean resource.

Color
colorResource(@ColorRes id: Int)

Load a color resource.

Dp
dimensionResource(@DimenRes id: Int)

Load a boolean resource.

Typeface
fontResource(fontFamily: FontFamily)

Synchronously load an font from FontFamily.

ImageAsset
imageResource(@DrawableRes id: Int)

Synchronously load an image resource.

IntArray
integerArrayResource(@ArrayRes id: Int)

Load an array of integer resource.

Int
integerResource(@IntegerRes id: Int)

Load an integer resource.

DeferredResource<Typeface>
loadFontResource(fontFamily: FontFamily, pendingFontFamily: FontFamily? = null, failedFontFamily: FontFamily? = null)

Load the FontFamily in background thread.

DeferredResource<Typeface>
loadFontResource(fontFamily: FontFamily, pendingTypeface: Typeface? = null, failedTypeface: Typeface? = null)

Load the FontFamily in background thread.

DeferredResource<ImageAsset>
loadImageResource(id: Int, pendingImage: ImageAsset? = null, failedImage: ImageAsset? = null)

Load the image in background thread.

DeferredResource<VectorAsset>
loadVectorResource(id: Int, pendingResource: VectorAsset? = null, failedResource: VectorAsset? = null)

Load the vector drawable in background thread.

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

Load a string resource.

String
stringResource(@StringRes id: Int)

Load a string resource.

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

Load a string resource with formatting.

VectorAsset
vectorResource(@DrawableRes id: Int)

Effect used to load a VectorAsset from an Android resource id This is useful for querying top level properties of the VectorAsset such as it's intrinsic width and height to be able to size components based off of it's dimensions appropriately

Top-level functions

booleanResource

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

Load a boolean resource.

Parameters
id: Int the resource identifier
Return
the boolean associated with the resource

colorResource

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

Load a color resource.

Parameters
id: Int the resource identifier
Return
the color associated with the resource

dimensionResource

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

Load a boolean resource.

Parameters
id: Int the resource identifier
Return
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
Return
the decoded image data associated with the resource

imageResource

@Composable fun imageResource(@DrawableRes id: Int): ImageAsset

Synchronously load an image resource.

Note: This API is transient and will be likely removed for encouraging async resource loading.

Parameters
id: Int the resource identifier
Return
the decoded image data associated with the resource

integerArrayResource

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

Load an array of integer resource.

Parameters
id: Int the resource identifier
Return
the integer array associated with the resource

integerResource

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

Load an integer resource.

Parameters
id: Int the resource identifier
Return
the integer associated with the resource

loadFontResource

@Composable fun loadFontResource(
    fontFamily: FontFamily,
    pendingFontFamily: FontFamily? = null,
    failedFontFamily: FontFamily? = null
): DeferredResource<Typeface>

Load the FontFamily in background thread.

Until font family loading complete, this function returns deferred Typeface with PendingResource. Once loading finishes, recompose is scheduled and this function will return deferred Typeface resource with LoadedResource or FailedResource.

import androidx.compose.foundation.Text
import androidx.compose.ui.res.loadFontResource
import androidx.compose.ui.text.font.LoadedFontFamily

loadFontResource(
    FontFamily.Default,
    pendingFontFamily = FontFamily.Cursive,
    failedFontFamily = FontFamily.SansSerif
).resource.resource?.let {
    Text(
        text = "Hello, World",
        fontFamily = LoadedFontFamily(it)
    )
}
Parameters
fontFamily: FontFamily the font family to be loaded
pendingFontFamily: FontFamily? = null an optional resource to be used during loading instead. Only FontFamily that can be loaded synchronously can be used as a pendingFontFamily.
failedFontFamily: FontFamily? = null an optional resource to be used during loading instead. Only FontFamily that can be loaded synchronously can be used as a failedFontFamily.
Exceptions
IllegalArgumentException if FontFamily other than synchronously loadable ones are passed as an argument of pendingFontFamily or failedFontFamily.

loadFontResource

@Composable fun loadFontResource(
    fontFamily: FontFamily,
    pendingTypeface: Typeface? = null,
    failedTypeface: Typeface? = null
): DeferredResource<Typeface>

Load the FontFamily in background thread.

Until font family loading complete, this function returns deferred Typeface with PendingResource. Once loading finishes, recompose is scheduled and this function will return deferred Typeface resource with LoadedResource or FailedResource.

import androidx.compose.foundation.Text
import androidx.compose.ui.res.fontResource
import androidx.compose.ui.res.loadFontResource
import androidx.compose.ui.text.font.LoadedFontFamily

loadFontResource(
    FontFamily.Default,
    pendingTypeface = fontResource(FontFamily.Cursive),
    failedTypeface = fontResource(FontFamily.SansSerif)
).resource.resource?.let {
    Text(
        text = "Hello, World",
        fontFamily = LoadedFontFamily(it)
    )
}
Parameters
fontFamily: FontFamily the font family to be loaded
pendingTypeface: Typeface? = null an optional resource to be used during loading instead.
failedTypeface: Typeface? = null an optional resource to be used during loading instead.
Exceptions
IllegalArgumentException if FontFamily other than synchronously loadable ones are passed as an argument of pendingFontFamily or failedFontFamily.

loadImageResource

@Composable fun loadImageResource(
    id: Int,
    pendingImage: ImageAsset? = null,
    failedImage: ImageAsset? = null
): DeferredResource<ImageAsset>

Load the image in background thread.

Until resource loading complete, this function returns deferred image resource with PendingResource. Once the loading finishes, recompose is scheduled and this function will return deferred image resource with LoadedResource or FailedResource.

Parameters
id: Int the resource identifier
pendingImage: ImageAsset? = null an optional image to be used during loading instead.
failedImage: ImageAsset? = null an optional image to be used if image loading failed.
Return
the deferred image resource.

loadVectorResource

@Composable fun loadVectorResource(
    id: Int,
    pendingResource: VectorAsset? = null,
    failedResource: VectorAsset? = null
): DeferredResource<VectorAsset>

Load the vector drawable in background thread.

Until resource loading complete, this function returns deferred vector drawable resource with PendingResource. Once the loading finishes, recompose is scheduled and this function will return deferred vector drawable resource with LoadedResource or FailedResource.

Parameters
id: Int the resource identifier
pendingResource: VectorAsset? = null an optional resource to be used during loading instead.
failedResource: VectorAsset? = null an optional resource to be used if resource loading failed.
Return
the deferred vector drawable resource.

stringArrayResource

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

Load a string resource.

Parameters
id: Int the resource identifier
Return
the string data associated with the resource

stringResource

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

Load a string resource.

Parameters
id: Int the resource identifier
Return
the string data associated with the resource

stringResource

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

Load a string resource with formatting.

Parameters
id: Int the resource identifier
vararg formatArgs: Any the format arguments
Return
the string data associated with the resource

vectorResource

@Composable fun vectorResource(@DrawableRes id: Int): VectorAsset

Effect used to load a VectorAsset from an Android resource id This is useful for querying top level properties of the VectorAsset such as it's intrinsic width and height to be able to size components based off of it's dimensions appropriately

Note: This API is transient and will be likely removed for encouraging async resource loading.