39
votes
fun createListItem(itemIndex: Int) {
Padding(left = 8.dp, right = 8.dp, top = 8.dp, bottom = 8.dp) {
    FlexRow(crossAxisAlignment = CrossAxisAlignment.Center) {
        expanded(1.0f) {
            Text("Item $itemIndex")
        }
        inflexible {
            Button(
                "Button $itemIndex",
                style = ContainedButtonStyle(),
                onClick = {
                    Toast.makeText(
                        this@MainActivity,
                        "Item name $itemIndex",
                        Toast.LENGTH_SHORT
                    ).show()
                })

        }
    }
  }
}

I try to make Toast in a normal way. but I got the error I tried a lot of multiples source but failed.

10
did you add the "@Composable" annotation? - Spark.Bao
yes did. @Composable annotation - Mr. Tayyab MuGhal
You might want to explain exactly what your problem was. I have used Toast in onClick handlers in Compose without a problem. If you were getting a compile error, please provide the complete details of the error. If you were getting a runtime error, please edit your question and post the stack trace. - CommonsWare
FYI: while the answers below are valid, the compose team now recommends using Snackbar over Toast in Compose: kotlinlang.slack.com/archives/CJLTWPH7S/…. - Raman

10 Answers

73
votes

Update March 2021: The previous answer has been deprecated. You should now use:

val context = LocalContext.current

Previous answer for reference:

You can access to context with define ambientContext.

Example:

val context = ContextAmbient.current
20
votes

ContextAmbient and AmbientContext is deprecated

You can replace them with

val context = LocalContext.current
16
votes

ContextAmbient.current is deprecated as of alpha-09.

AmbientContext.current is deprecated. I think as of alpha-11.

LocalContext.current is how you get the context in a composable now.

14
votes

The way to do this has been updated. It's now:

val context = LocalContext.current

LocalContext docs

2
votes

For getting context in jetpack compose:

val context = ContextAmbient.current

Working on 0.1.0-dev14

How to use it in TOAST:

@Composable
fun cardViewImplementer(item: Int) {
   val context = ContextAmbient.current
   Card(
     shape = RoundedCornerShape(10.dp),
     modifier = Modifier.padding(10.dp)
   ) {
     Box(
        modifier = Modifier
            .fillMaxWidth()
            .drawShadow(5.dp)
            .clickable(onClick = {
                Toast.makeText(context, "Clicked $item", Toast.LENGTH_SHORT).show()
            }), children = {

        })
}

For accessing the Resource:

Text("Read this string: "+context.getString(R.string.name))
2
votes

Issues with compose_version = '1.0.0-alpha12' ? AmbientContext is now LocalContext

2
votes

ContextAmbient.current has been deprecated, use val context = LocalContext.current instead.

1
votes
val context = LocalContext.current
Toast.makeText(context,"Hello Compose",Toast.LENGTH_LONG).show()
1
votes

I think your never show Toast normal way. Jetpack Compose uses a custom Kotlin compiler plugin to transform these composable functions into the app's UI elements. For example, the Text() function is defined by the Compose UI library. Jetpack Compose is in very early stages of development. You can see all repo of Jetpack compose with sample and integrations test check below link

https://android.googlesource.com/platform/frameworks/support/+/refs/heads/androidx-master-dev/ui

0
votes

LocalContext.current - is the right approach. But the problem is you can't use LocalContext.current inside @Composable function

You need to create separate function to use Context

Sample code

@Composable
fun DoneButton(){
    val context = LocalContext.current
    Button(onClick = { showToast(context,"Button clicked")}) {
        Text(name = "Done")
    }
}

fun showToast(context: Context, msg:String){
    Toast.makeText(context,msg,Toast.LENGTH_LONG).show()
}