Chapter 19. Integrating Compose with views: Perfect Harmony

image

You get the best results when things work together.

So far, you’ve learned how to build a UI using views or composables. But what if you want to use both? In this chapter, you’ll find out how to get the best of both worlds by adding composables to a View-based UI. You’ll discover techniques for making composables work with view models. You’ll even find out how to make them respond to LiveData updates. By the end of the chapter, you’ll have all the tools you need to use composables with views, or even migrate to a pure Compose UI.

You can add composables to View-based UIs

In the previous chapter, you learned how to implement a Compose UI by building a brand new Temperature Converter app. Instead of adding views to a layout file, you created the UI by calling composables in the activity’s Kotlin code.

There are times, however, when you might want to use views and composables in the same UI. This may be because you want to use components that are only available as views or composables, for example, or you want to migrate parts of your app to Compose.

The great news is that you can add composables to a UI that’s defined in a layout file. We’ll show you how this is done by taking the Guessing Game app we created earlier in the book, and migrating it to Compose.

The Guessing Game app revisited

As we’re ...

Get Head First Android Development, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.