With XML, we had the option to use tools:
for designing by using placeholders when real data is not available. Do we have something similar in Jetpack Compose?
I know I can pass sample data to my composable in a dedicated preview function. But for instance, when an image source is a URL (that loads by Coil, Glide.. ), even if I pass a sample URL, it can't be loaded in preview. A practical solution for that could save development time.
If you are using Coil...
implementation("io.coil-kt:coil-compose:2.2.2")
... now you can preview with a placeholder in debug mode only this way:
AsyncImage(
model = "https://www.example.com/image.jpg",
placeholder = debugPlaceholder(R.drawable.debugPlaceholder),
contentDescription = stringResource(R.string.description)
)
And create an util method:
@Composable
fun debugPlaceholder(@DrawableRes debugPreview: Int) =
if (LocalInspectionMode.current) {
painterResource(id = debugPreview)
} else {
null
}
You can see more here: https://coil-kt.github.io/coil/compose/
Just as an update on cd1 Answer:
rememberCoilPainter is renamed to rememberImagePainter and its arguments changed
More info about the changes:
source: https://coil-kt.github.io/coil/compose/
Regarding placeholder visible in preview, the code is:
Image(
painter = rememberImagePainter(
data = "https://www.example.com/image.jpg",
builder = {
placeholder(R.drawable.placeholder)
}
),
contentDescription = "some description",
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With