Mr. Tayyab MuGhal
Mr. Tayyab MuGhal

Reputation: 2321

How to get Context in Jetpack Compose

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.

Upvotes: 217

Views: 146474

Answers (11)

Ranjithkumar
Ranjithkumar

Reputation: 18346

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

You need to create separate function to use Context:

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

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

Upvotes: 33

Nazanin Nasab
Nazanin Nasab

Reputation: 625

You can use the LocalUriHandler:

val handler = LocalUriHandler.currenthandler 

Button( 
    onClick = { handler.openUri("https://www.stackoverflow.com") } 
) {    
     Text("Open") 
} 

Upvotes: 2

Ahmed Bakhet
Ahmed Bakhet

Reputation: 3214

ContextAmbient and AmbientContext has been deprecated.

You can replace them with LocalContext

Example:

val context = LocalContext.current

Upvotes: 97

Anas Mehar
Anas Mehar

Reputation: 2835

ContextAmbient and AmbientContext is deprecated

Update

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

val context = LocalContext.current

Upvotes: 37

brucemax
brucemax

Reputation: 962

Some useful if you need get context as Activity from last Android Studio template:

val view = LocalView.current
(view.context as Activity).<activity method>

Better solution

fun Context.getActivity(): Activity? = when (this) {
    is Activity -> this
    is ContextWrapper -> baseContext.getActivity()
    else -> null
}

val activity = LocalContext.current.getActivity()

Upvotes: 10

Brian Mutiso
Brian Mutiso

Reputation: 400

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

Upvotes: 14

Ryan M
Ryan M

Reputation: 20119

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

val context = LocalContext.current

LocalContext docs

Upvotes: 36

mitch
mitch

Reputation: 917

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.

Upvotes: 40

Raka Adi Nugroho
Raka Adi Nugroho

Reputation: 4253

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

Upvotes: 397

Gianluca Veschi
Gianluca Veschi

Reputation: 1608

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

Upvotes: 5

Ali Azaz Alam
Ali Azaz Alam

Reputation: 1868

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))

Upvotes: 6

Related Questions