Search code examples
androidimagekotlinandroid-jetpack-composecoil

android compose onclick asyncimage change picture


Like title said it needs to update image with new picture in Android Compose, using Coil.

 AsyncImage(
        model = URL,
        contentDescription = "",
        modifier = Modifier.clickable(onClick = {
            //Maybe here to add code?
        })
    )

What I need is to load new picture from link into existing AsyncImage.


Solution

  • Create a mutable state that you update with new url:

    var model by remember { mutableStateOf(URL) }
    AsyncImage(
        model = model,
        contentDescription = "",
        modifier = Modifier.clickable(onClick = {
            model = newUrl
        })
    )