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.

What was checked, and how: read this first
Checked in the docs (one web search, summaries only): hosting Compose with 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).

<!-- res/layout/activity_main.xml (a typical old-style layout) --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/greeting_title" /> <Button android:id="@+id/open_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/open_details" /> </LinearLayout>
// The Views way: find views by id, then act on them setContentView(R.layout.activity_main) findViewById<Button>(R.id.open_button).setOnClickListener { /* open details */ }

The same screen in Compose (Chapters 3 and 8):

// The same screen in Compose @Composable fun MainScreen(onOpen: () -> Unit) { Column(modifier = Modifier.fillMaxSize().padding(16.dp)) { Text(stringResource(R.string.greeting_title)) Button(onClick = onOpen) { Text(stringResource(R.string.open_details)) } } }
XML ViewsCompose
LinearLayout (vertical, horizontal)Column, Row
FrameLayoutBox
ConstraintLayoutNested layouts, or the ConstraintLayout library for Compose
TextView, Button, EditTextText, Button, TextField
android:padding, layout_marginModifier.padding(...)
android:id plus findViewById or view bindingNo ids: the data is the source of truth (Chapter 4)
RecyclerView with an Adapter and ViewHolderLazyColumn (Chapter 6)
A FragmentA 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:

<!-- res/layout/activity_main.xml: a ComposeView sits in the layout like any other view --> <androidx.compose.ui.platform.ComposeView android:id="@+id/compose_view" android:layout_width="match_parent" android:layout_height="wrap_content" />
// In the Activity (a ComponentActivity or AppCompatActivity) setContentView(R.layout.activity_main) findViewById<ComposeView>(R.id.compose_view).setContent { ComposeHelloTheme { Greeting("from Compose") } }
  • (docs) The ComposeView must be attached to a view-tree lifecycle owner; ComponentActivity, FragmentActivity and AppCompatActivity all provide one.
  • (docs) If a layout has more than one ComposeView, give each a unique ID, or savedInstanceState will not work properly.
  • (docs) In a Fragment, always set the DisposeOnViewTreeLifecycleDestroyed composition strategy:
// In a Fragment's onCreateView return ComposeView(requireContext()).apply { setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) setContent { ComposeHelloTheme { Greeting("from a Fragment") } } }

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:

@Composable fun ClassicTextView() { AndroidView( factory = { context -> TextView(context).apply { text = "A classic TextView inside Compose" } } ) }

Which Should I Use?

SituationChoice
A new appCompose only
An old app you are adding a screen toCompose 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 versionAndroidView
Reading or fixing an old XML screenLeave it as XML unless you are migrating it on purpose
Compared with the Classic UI course
Android Development Fundamentals 3 (XML layouts and Views), 5 (RecyclerView) and 6 (Fragments) are the chapters this appendix summarises. They are still the right place to learn the old pieces in depth, and they remain accurate for View-based projects.

Try It Yourself

Exercise 1

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 solution
Exercise 2

Create 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.

๐Ÿ“„ View solution
Exercise 3

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.

๐Ÿ“„ View solution
Course Complete
You have covered a first Compose app (1), the Activity lifecycle (2), layouts (3), state (4), interaction and Intents (5), lists (6), navigation (7), resources and themes (8) and how the old XML world fits beside it (9). Next in the Android track: Android Development with Compose: Architecture & Data (android5, not yet written), and Personal Catalogue: Android (Kotlin & Compose), a project course that applies all of this.
Not Yet Verified
Checked in the docs (one web search, summaries only): 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).