ποΈ CMPToast is a lightweight and easy-to-use library for displaying toast messages in Compose Multiplatform projects.
- Cross-platform support: Works seamlessly on Android and iOS.
- Composable API: Designed to integrate naturally into Jetpack Compose.
- Customizable: Customize your toast message with text, duration, and appearance.
To use CMPToast in your Compose Multiplatform project, add the following dependency to your build.gradle.kts
:
commonMain.dependencies {
implementation("network.chaintech:cmptoast:1.0.7")
}
Note : Create an Application Class and add the following line (Otherwise you might face context error):
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
multiplatform.network.cmptoast.AppContext.apply { set(applicationContext) }
}
}
Also if you are getting Manifest Merger Failed issue, add the following to your Android Manifest file:
tools:replace="android:name"
tools:replace="android:allowBackup"
To display a simple toast:
showToast(
message = "This is a Toast"
)
To display a toast add the ToastHost
to your main composable inside your wasmJs or jvm target:
Checkout the sample app for implementation
// For jvmMain target (..jvmMain/main.kt)
fun main() = application {
Window(
title = "CMPToast Demo",
state = rememberWindowState(width = 800.dp, height = 600.dp),
onCloseRequest = ::exitApplication,
) {
window.minimumSize = Dimension(150, 150)
App()
ToastHost() // *** Important : Include this line to show the ToastHost
}
}
// For wasmJs target (..wasmJsMain/main.kt)
@OptIn(ExperimentalComposeUiApi::class)
fun main() {
ComposeViewport(document.body!!) {
App()
ToastHost() // *** Important : Include this line to show the ToastHost
}
}
Watch the demo below to see CMPToast in action:
To show a short-duration toast:
showToast(
message = "This is Short Toast",
backgroundColor = Color.White,
textColor = Color.Black,
duration = ToastDuration.Short
)
To show a long-duration toast:
showToast(
message = "This is Long Toast",
backgroundColor = Color.White,
textColor = Color.Black,
duration = ToastDuration.Long
)
To show toast at top of the screen:
showToast(
message = "This is Top Toast",
backgroundColor = Color.White,
textColor = Color.Black,
gravity = ToastGravity.Top
)
To show toast at center of the screen:
showToast(
message = "This is Center Toast",
backgroundColor = Color.White,
textColor = Color.Black,
gravity = ToastGravity.Center
)
To show a colored toast:
showToast(
message = "This is Colored Toast",
backgroundColor = Color(0xFF27AD9D)
)
To show a toast with icon:
val resource = Res.drawable.ic_profile // Profile Icon
val iconBitmap = resource.let { imageResource(it) }
showToast(
message = "This is Icon Toast",
backgroundColor = Color.White,
textColor = Color.Black,
gravity = ToastGravity.Center,
icon = iconBitmap,
iconSizeDp = 20
)
Property | Type | Description |
---|---|---|
message | String |
The message to be displayed in the toast. |
icon | ImageBitmap? |
Optional icon to be displayed alongside the message. |
iconSizeDp | Dp |
The size of the icon in dp. |
gravity | ToastGravity |
The position of the toast (e.g., Top, Center, Bottom). |
backgroundColor | Color |
The background color of the toast. |
textColor | Color |
The color of the message text. |
duration | ToastDuration |
The duration of the toast (Short or Long). |
padding | ToastPadding? |
Internal padding for the toast content. |
textSize | TextUnit |
The size of the text. |
topPadding | Int |
Padding between toast and top of the screen. |
bottomPadding | Int |
Padding between toast and bottom of the screen. |
cornerRadius | Int? |
For rounding the corners of the toast. |
CMPToast currently supports the following platforms:
- Android π€
- iOS π
For an in-depth guide and detailed explanation, check out our comprehensive Medium Blog Post.
Stay connected and keep up with our latest innovations! πΌ Let's innovate together!
Copyright 2023 Mobile Innovation Network
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.