What is the difference between “remember” and “mutableState” in android jetpack compose?

remember is a composable function that can be used to cache expensive operations. You can think of it as a cache which is local to your composable. val state: Int = remember { 1 } The state in the above code is immutable. If you want to change that state and also update the UI, … Read more

How to use Compose inside Fragment?

setContent on ViewGroup is now deprecated. The below is accurate as of Compose v1.0.0-alpha01. For pure compose UI Fragment: class ComposeUIFragment : Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return ComposeView(requireContext()).apply { setContent { Text(text = “Hello world.”) } } } } For hybrid compose UI Fragment – … Read more

How to disable ripple effect when clicking in Jetpack Compose

Short answer: to disable the ripple pass null in the indication parameter in the clickable modifier: val interactionSource = remember { MutableInteractionSource() } Column { Text( text = “Click me without any ripple!”, modifier = Modifier .clickable( interactionSource = interactionSource, indication = null ) { /* doSomething() */ } ) Why it doesn’t work with … Read more

How to load Image from drawable in Jetpack compose?

You can use the painterResource function: Image(painterResource(R.drawable.ic_xxxx),”content description”) The resources with the given id must point to either fully rasterized images (ex. PNG or JPG files) or VectorDrawable xml assets. It means that this method can load either an instance of BitmapPainter or VectorPainter for ImageBitmap based assets or vector based assets respectively. Example: Card( … Read more

How to close the virtual keyboard from a Jetpack Compose TextField?

You can use the LocalSoftwareKeyboardController class to control the current software keyboard and then use the hide method: var text by remember { mutableStateOf(TextFieldValue(“Text”)) } val keyboardController = LocalSoftwareKeyboardController.current TextField( value = text, onValueChange = { text = it }, label = { Text(“Label”) }, keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done), keyboardActions = KeyboardActions( onDone = … Read more

Jetpack Compose – Column – Gravity center

You can use these parameters: horizontalAlignment = the horizontal gravity of the layout’s children. verticalArrangement= the vertical arrangement of the layout’s children Something like: Column( modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { Text( text = “First item”, modifier = Modifier.padding(16.dp) ) Text( text = “Second item”, modifier = Modifier.padding(16.dp) ) Text( … Read more