Android Jetpack Compose width / height / size modifier vs requiredWidth / requiredHeight / requiredSize

The difference is that plain modifiers like width() take into account layout Constraints while required modifiers like requiredWidth() ignore them. You can think of constrains as min/max width/height of a measured element. To better understand how layout modifiers and constraints work take a look at this post. Let’s see an example of width modifier, height … Read more

Jetpack Compose Smart Recomposition

To have smart recomposition scopes play a pivotal role. You can check Vinay Gaba’s What is “donut-hole skipping” in Jetpack Compose? article. Leland Richardson explains in this tweet as The part that is “donut hole skipping” is the fact that a new lambda being passed into a composable (ie Button) can recompose without recompiling the … Read more

Why does mutableStateOf without remember work sometimes?

It’s a feature of Compose about scoping and smart recomposition. You can check my detailed answer here. What really makes your whole Composable to recompose is Column having inline keyword. @Composable inline fun Column( modifier: Modifier = Modifier, verticalArrangement: Arrangement.Vertical = Arrangement.Top, horizontalAlignment: Alignment.Horizontal = Alignment.Start, content: @Composable ColumnScope.() -> Unit ) { val measurePolicy … Read more

Jetpack Compose collapsing toolbar

I found a solution created by Samir Basnet (from Kotlin Slack Channel) which was useful for me, I hope it helps someone else… @Composable fun CollapsingEffectScreen() { val items = (1..100).map { “Item $it” } val lazyListState = rememberLazyListState() var scrolledY = 0f var previousOffset = 0 LazyColumn( Modifier.fillMaxSize(), lazyListState, ) { item { Image( … Read more

Jetpack Compose Text hyperlink some section of the text

For a complete answer you can use ClickableText which returns the position of text, and UriHandler to open URI in a browser. val annotatedLinkString: AnnotatedString = buildAnnotatedString { val str = “Click this link to go to web site” val startIndex = str.indexOf(“link”) val endIndex = startIndex + 4 append(str) addStyle( style = SpanStyle( color … Read more

Pass Parcelable argument with compose navigation

Warning: Ian Lake is an Android Developer Advocate and he says in this answer that pass complex data structures is an anti-pattern (referring the documentation). He works on this library, so he has authority on this. Use the approach below by your own. Edit: Updated to Compose Navigation 2.4.0-beta07 Seems like previous solution is not … Read more

How can I detect a click with the view behind a Jetpack Compose Button?

When button finds tap event, it marks it as consumed, which prevents other views from receiving it. This is done with consumeDownChange(), you can see detectTapAndPress method where this is done with Button here To override the default behaviour, you had to reimplement some of gesture tracking. List of changes comparing to system detectTapAndPress: I … Read more

How to handle one-shot operations in Jetpack Compose?

I prefer using SharedFlow for a job like this: class OneShotOperationViewModel : ViewModel() { private val _toastMessage = MutableSharedFlow<String>() val toastMessage = _toastMessage.asSharedFlow() fun sendMessage(message: String) { viewModelScope.launch { _toastMessage.emit(message) } } } @Composable fun TestScreen() { val context = LocalContext.current val viewModel = viewModel<OneShotOperationViewModel>() LaunchedEffect(Unit) { viewModel .toastMessage .collect { message -> Toast.makeText( context, … Read more