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

Android Jetpack Navigation with ViewPager and TabLayout

Experimented with different approaches to handle TabLayout with Jetpack Navigation. But hit issues like having a full history of switching between tabs multiple times etc. Browsing known Google Android Issues before raising a request for a demo, I found this existing issue. Its status is Closed marked as Intended Behavior with the following explanation: Navigation … Read more

How to navigate from a composable to an activity or a fragment in Jetpack Compose?

In newer version of compose use LocalContext. In older versions (1.0.0-alpha08 and before) use AmbientContext: @Composable fun MainScreen() { val context = LocalContext.current Button(onClick = { context.startActivity(Intent(context, ListActivity::class.java)) }) { Text(text = “Show List”) } }

How to do latest jetpack “View binding” in adapter, bind the views?

You can use static bind method of ViewBinding to create binding from already existing layout. Add it as a property to viewholder: class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val binding = ItemListBinding.bind(view) } then you can access all the views through the binding field, for example: override fun onBindViewHolder(holder: ViewHolder, position: Int) { with(holder) { … Read more