Skip to content

Instantly share code, notes, and snippets.

@smartherd
Created May 26, 2022 05:21
Show Gist options
  • Save smartherd/333ddba04b2758dd4ce6d47d12b94836 to your computer and use it in GitHub Desktop.
Save smartherd/333ddba04b2758dd4ce6d47d12b94836 to your computer and use it in GitHub Desktop.
Toast Message in Android Jetpack Compose | Source Code
package com.sriyank.composecomponents
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.sriyank.composecomponents.ui.theme.ComposeComponentsTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ComposeComponentsTheme {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
val context = LocalContext.current
Button(
onClick = {
Toast.makeText(context, "This is a Toast. Yay!", Toast.LENGTH_SHORT).show()
},
) {
Text("Show Toast")
}
}
}
}
}
}
@smartherd
Copy link
Author

This is the Output on click of the Show Toast button.

Screenshot_20220526_105221assaas

Complete Screenshot
Screenshot_20220526_105221

@RisterAlline
Copy link

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment