android-jetpack-compose
ViewTreeLifecycleOwner not found from DecorView@2da7146[MyActivity]
Try updating the dependency of AppCompat to rc01 version. This solved the problem for me. implementation ‘androidx.appcompat:appcompat:1.3.0-rc01’
Sharing viewModel within Jetpack Compose Navigation
Consider passing your activity to viewModel() fun as viewModelStoreOwner parameter since ComponentActivity implements ViewModelStoreOwner interface: val viewModel: ConversionViewModel = viewModel(LocalContext.current as ComponentActivity) This code will return the same instance of ConversionViewModel in all your destinations.
What is Scaffold? Jetpack compose
Scaffold allows you to implement a UI with the basic Material Design layout structure. Scaffold provides slots for the most common top-level Material components, such as TopAppBar, BottomAppBar, FloatingActionButton, and Drawer. Something like: val scaffoldState = rememberScaffoldState() // Create a coroutineScope for the animation val coroutineScope = rememberCoroutineScope() Scaffold( scaffoldState = scaffoldState, drawerContent = { … Read more
How to inject a ViewModel into a composable function using Hilt (Jetpack Compose)
From version androidx.hilt:hilt-navigation-compose:1.0.0-alpha02 you can inject view model into Composable functions by: hiltViewModel<ViewModelType>() Example: @Composable fun LoginScreen(viewModel: LoginViewModel) {} LoginScreen( viewModel = hiltViewModel<LoginViewModel>() ) Android Developer Documentation compose and hilt UPDATE: import androidx.hilt.navigation.compose.hiltViewModel @Composable fun LoginScreen( viewModel: LoginViewModel = hiltViewModel() ){ val videos=vm.watchLater.observeAsState() val context= LocalContext.current }
Jetpack Compose: Update composable when list changes
You need to use a MutableStateList<T> so that Compose can automatically recompose when the state changes. From official doc: Caution: Using mutable objects such as ArrayList<T> or mutableListOf() as state in Compose will cause your users to see incorrect or stale data in your app. In your code use val favourites = remember { mutableStateListOf<Track>()} … Read more
How to add extra colors into MaterialTheme in Android Jetpack Compose?
Just a small change to Valeriy Katkov’s answer In some versions of the android studio, the following code will not work @Composable val Colors.myExtraColor: Color get() = if (isLight) Color.Red else Color.Green @Composable fun ExtraColorExample() { Text( text = “test”, color = MaterialTheme.colors.myExtraColor // <– the newly added color ) } It will show an … Read more
How clear focus for BasicTextField in Jetpack Compose?
To clear focus from the currently focused component you can use the FocusManager.clearFocus method: val focusRequester = remember { FocusRequester() } val focusManager = LocalFocusManager.current var value by rememberSaveable { mutableStateOf(“initial value”) } BasicTextField( value = value, onValueChange = { value = it }, decorationBox = { innerTextField -> Row( Modifier .background(Color.LightGray, RoundedCornerShape(percent = 30)) … Read more
Fill height for child in Row
You have to apply Modifier.height(IntrinsicSize.Min) to the Row and the fillMaxHeight() modifier to the 2nd Box. Something like: Card(shape = RoundedCornerShape(8.dp)) { Row( modifier = Modifier.height(IntrinsicSize.Min) //Intrinsic measurement ) { Box( Modifier .background(Color.Yellow) .weight(1f) ) { //Box(Modifier.height(50.dp)) } Box( Modifier .width(20.dp) .fillMaxHeight() //<— fill max height .background(Color.Green) ) { //…….. } } } As explained … Read more
How to create GridView using Jetpack Compose
With 1.x.y the LazyVerticalGrid composable provides experimental support for displaying items in a grid. val numbers = (0..20).toList() LazyVerticalGrid( columns = GridCells.Fixed(4) ) { items(numbers.size) { Column(horizontalAlignment = Alignment.CenterHorizontally) { Text(text = “Number”) Text(text = ” $it”,) } } } The columns = GridCells.Fixed(4) would mean that there are 4 columns 1/4 of the parent … Read more