Member-only story
Things to know about in Jetpack Compose.
3 min readSep 25, 2023
In this article we will see some common things in jetpack compose
1). Get Context of the current screen
@Composable
fun FindContext() {
val context = LocalContext.current
Toast.makeText(context,"Hey",Toast.LENGTH_LONG).show()
}
We get the context through LocalContext.currrent
composition local
2). Auto Focus on TextField
Get auto focus on textfield , when it visible for the first time.
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FocusOnTextField(
modifier: Modifier = Modifier
) {
val focusRequester = remember { FocusRequester() }
LaunchedEffect(key1 = Unit) {
focusRequester.requestFocus()
}
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
TextField(
value = "",
onValueChange = {},
placeholder = { Text(text = "Enter username") },
modifier = Modifier.focusRequester(focusRequester)
)
}
}