Simple Toast library for Compose Multiplatform targeting Android, iOS, and JVM (Desktop).
This library is implemented using the method in https://proandroiddev.com/how-to-show-toasts-in-compose-multiplatform-android-ios-desktop-with-expect-actual-85c630d46d06
// build.gradle.kts
kotlin {
sourceSets {
commonMain.dependencies {
implementation("id.neotica:toast:0.3.1") // Check for the latest version
}
}
}On your MainActivity, add setActivityProvider
import id.neotica.toast.setActivityProvider
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
super.onCreate(savedInstanceState)
setActivityProvider { this } // add this
setContent {
App()
}
}
}On your main.kt, add setComposeWindowProvider
import id.neotica.toast.setComposeWindowProvider
fun main() = application {
Window(
onCloseRequest = ::exitApplication,
title = "Notes",
) {
setComposeWindowProvider { window } // add this
App()
}
}To use this after the setup,
- just add a mutable variable of
ToastManager() - call the variable just like the regular toast.
- you can also add the duration, by calling
ToastDurationenum, which I provides with two option: SHORT and LONG. The default value is SHORT.
@Composable
fun SomeView() {
val toastManager by remember { mutableStateOf(ToastManager()) } // 1. add this
Button(
onClick = {
toastManager.showToast("This is toast.") // 2. Implement just like regular toast
toastManager.showToast("Note clicked: ${note.id}", ToastDuration.LONG) // 3. You can also add duration
}
) { Text("Demo") }
}