Legacy Appendix
Android Development Fundamentals with Compose
Course 4 ยท Chapter 9 ยท XML, Views and Fragments: the Old World
Everything so far used Compose. But most Android apps in the world were written before Compose existed, and many tutorials, libraries and job tasks still use XML layouts, Views and Fragments. You do not need to write them for a new app, but you do need to read them, and often to make them live next to Compose code. This last chapter shows how the old pieces map to the new ones, and how to mix the two.
ComposeView and setContent(), the lifecycle owner requirement, the fragment DisposeOnViewTreeLifecycleDestroyed advice, unique IDs for several ComposeViews,
AndroidViewBinding for small legacy XML layouts inside Compose, and that Compose and Views can coexist during a migration. Not found in the docs search: how View-based XML layouts and Fragments themselves work (general knowledge from the older course), and the AndroidView function itself.
All code and XML are unrun (no Kotlin compiler on the machine used to write this). Nothing here was seen on your install.
Why the Old Pieces Still Matter
- Existing apps. (docs) The guidance for existing apps is that Compose and Views coexist in a codebase until the app is fully in Compose: you migrate a screen or a widget at a time.
- Libraries and tutorials. Many are written for Views; you need to recognise them (general knowledge).
- Android Development Fundamentals (the XML course,
android1) remains on this site for exactly that reason.
Reading an XML Layout
(general knowledge, from the older course) A layout file describes a tree of views with attributes. Each android:id="@+id/..." names a view so code can find it; layout_width and layout_height are required on every view; @string/... points at a string resource (Chapter 8).
The same screen in Compose (Chapters 3 and 8):
| XML Views | Compose |
|---|---|
LinearLayout (vertical, horizontal) | Column, Row |
FrameLayout | Box |
ConstraintLayout | Nested layouts, or the ConstraintLayout library for Compose |
TextView, Button, EditText | Text, Button, TextField |
android:padding, layout_margin | Modifier.padding(...) |
android:id plus findViewById or view binding | No ids: the data is the source of truth (Chapter 4) |
RecyclerView with an Adapter and ViewHolder | LazyColumn (Chapter 6) |
| A Fragment | A composable screen, with a NavHost destination (Chapter 7) |
What a Fragment Was
(general knowledge) A Fragment is a reusable piece of a screen with its own lifecycle and its own layout, hosted inside an Activity; the Navigation Component swapped Fragments in and out of one Activity. In a Compose app that job is done by composable screens and a
NavHost, with no Fragments at all. You still meet Fragments in older code, in some libraries, and whenever you add Compose to an app that already uses them.
Compose Inside a View Layout
(docs) To add Compose to an existing layout or Fragment, use ComposeView and call its setContent(). Put the ComposeView in the XML like any other view, find it by ID, and call setContent:
- (docs) The
ComposeViewmust be attached to a view-tree lifecycle owner;ComponentActivity,FragmentActivityandAppCompatActivityall provide one. - (docs) If a layout has more than one
ComposeView, give each a unique ID, orsavedInstanceStatewill not work properly. - (docs) In a Fragment, always set the
DisposeOnViewTreeLifecycleDestroyedcomposition strategy:
Views Inside Compose
(docs) The other direction is for pieces that have no Compose version yet or are too big to rewrite. AndroidViewBinding (from the ui-viewbinding library) embeds an XML layout, and the docs say to use it only for smaller legacy layouts during an incremental migration, not for whole screens in a Compose-only app.
(general knowledge) For a single classic view there is AndroidView:
Which Should I Use?
| Situation | Choice |
|---|---|
| A new app | Compose only |
| An old app you are adding a screen to | Compose for the new screen, hosted with ComposeView if it must live inside a Fragment or XML layout |
| A Compose app that needs a widget with no Compose version | AndroidView |
| Reading or fixing an old XML screen | Leave it as XML unless you are migrating it on purpose |
Try It Yourself
Using the XML layout in this chapter, write its Compose equivalent from memory first, then compare with the chapter's version. List the XML attributes that have no direct counterpart and say why.
๐ View solutionCreate a project with an Empty Views Activity template, add a ComposeView to its layout, and show a Compose greeting inside it. Report what the template generated and anything that needed fixing.
In your Compose project, embed a classic TextView with AndroidView. Then write two sentences on when you would choose that over rewriting the widget in Compose.
android5, not yet written), and Personal Catalogue: Android (Kotlin & Compose), a project course that applies all of this.
ComposeView with setContent(); the view-tree lifecycle owner requirement; unique IDs; DisposeOnViewTreeLifecycleDestroyed in Fragments; AndroidViewBinding for small legacy XML layouts; Compose and Views coexisting in a migration.Not found in the docs search: View-based XML layouts and Fragments themselves;
AndroidView (general knowledge); the Views-to-Compose mapping table.Not verified: the imports; the Empty Views Activity template's current output (Exercise 2); every listing, layout and solution (unrun); the exercises (not run by me). Nothing was checked on your install.
Sources: Using Compose in Views (developer.android.com), Using Views in Compose (developer.android.com), Migrating to Jetpack Compose (developer.android.com codelab).