User Interaction
Android Development Fundamentals with Compose
Course 4 ยท Chapter 5 ยท Taps, Typing and Other Screens
A screen the user cannot touch is a poster. This chapter adds the three kinds of interaction every app needs: reacting to taps, taking typed input, and moving to another screen or app with an Intent. All of it follows the pattern from Chapter 4: an event changes state, and the UI is recomposed from the state.
TextField pattern (a value plus onValueChange that updates a remembered state), getting a context with LocalContext.current and calling startActivity, and
putExtra for extra data. Not found in the docs search: Button and onClick, clickable, Checkbox, and the difference between explicit and implicit Intents (all described from general knowledge). Newer text API:
the docs show a newer, state-based way to hold text-field state (TextFieldState) arriving and the value and onValueChange versions of the basic text field being deprecated in an alpha Compose UI release; I have not checked which API your project uses.
All code is unrun (no Kotlin compiler on the machine used to write this). Nothing was seen on your install.
Reacting to Taps
Compose has no setOnClickListener. You pass a lambda when you create the composable. Button takes an onClick; any other composable can be made tappable with the clickable modifier (general knowledge).
The lambda usually changes state, as count++ did in Chapter 4. The same pattern covers Checkbox and Switch: they receive a checked value and an onCheckedChange callback, exactly like a stateless composable (general knowledge).
Typed Input with TextField
(docs) A TextField shows the value you give it and reports a proposed new value through onValueChange. It does not change the text by itself: you store the new value in state, which recomposes the field with it.
That is state hoisting (Chapter 4) in its most common form.
OutlinedTextFieldis the Material 3 variant with an outline, andTextFieldthe filled one (general knowledge).- If you leave out
name = it, typing does nothing: the field redraws with the old value. This is the commonest beginner puzzle. rememberSaveablekeeps the typed text across rotation (Chapter 2).- The docs describe a newer
TextFieldState-based text field replacing thevalueandonValueChangestyle over time. If Android Studio marks the code above as deprecated, look up the current version in the docs.
Other Gestures
Taps are the 95% case. Compose has further modifiers and APIs for dragging, scrolling, long presses and swiping, which are not covered in this course (general knowledge; I did not research them).
Intents: Moving to Another Screen or App
An Intent is a message asking Android to start something. (general knowledge, not found by my search) Two kinds:
| Kind | What you name | Example |
|---|---|---|
| Explicit | The exact Activity class to start | Open your own DetailActivity |
| Implicit | An action and let Android find an app that can do it | Open a web address, share text, dial a number |
(docs) In Compose you get the context with LocalContext.current and call startActivity() on it, and you add extra data with putExtra(). An implicit example (the docs' sharing guide uses an action in the same way):
Passing Data to Another Activity
An explicit Intent names the class; putExtra attaches a value under a key, and the second Activity reads it from its own intent. The new Activity must also be declared in AndroidManifest.xml
(general knowledge; Android Studio's New Activity wizard normally adds it, which I have not checked).
Compared with the XML Way
| Views (Android Development Fundamentals) | Compose | |
|---|---|---|
| React to a tap | button.setOnClickListener { } after findViewById or view binding | onClick = { } or Modifier.clickable { } |
| Read typed text | editText.text.toString() | The state that onValueChange updates |
| Start another Activity | startActivity(Intent(...)) | The same, via LocalContext.current |
| Read extras | intent.getStringExtra(...) | The same |
onClick, event listeners and Intents. The Intent half is unchanged (same classes, same extras); the click and input half becomes lambdas and state.
Try It Yourself
Build the NameInput screen: a text field and a line that greets the typed name. Then deliberately remove name = it and describe what happens when you type.
Add a Checkbox labelled "I agree" and a Button that is enabled only when the box is ticked. (Hint: Button has an enabled parameter.)
Add a button that opens a web page with an implicit Intent. Then create a DetailActivity, declare it in the manifest, and send it the typed name from Exercise 1 with an explicit Intent and putExtra.
value plus onValueChange pattern for TextField; LocalContext.current with startActivity(); putExtra(); that a state-based text field API (TextFieldState) is arriving and the older value and onValueChange basic text field overloads are deprecated in an alpha release.Not found in the docs search:
Button and onClick, clickable, Checkbox and Switch parameters, OutlinedTextField, the explicit and implicit Intent distinction, the manifest entry and what the New Activity wizard does (all general knowledge).Not verified: whether
TextField with value and onValueChange is flagged as deprecated in your Compose version; the imports; the Uri.parse call; every listing and solution (unrun); the exercises (not run by me). Nothing was checked on your install.Sources: State and Jetpack Compose (developer.android.com), Send data to other apps (developer.android.com), Migrate to state-based text fields (developer.android.com), TextField API reference (developer.android.com).
LazyColumn, items and keys, item composables, click handling in a list and LazyRow, the Compose replacement for RecyclerView.