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.

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

// A Button has onClick built in Button(onClick = { /* do something */ }) { Text("Press me") } // Any composable can be made tappable with the clickable modifier Text( text = "Tap this text", modifier = Modifier.clickable { /* do something */ } )

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

var agreed by remember { mutableStateOf(false) } Row(verticalAlignment = Alignment.CenterVertically) { Checkbox(checked = agreed, onCheckedChange = { agreed = it }) Text("I agree") }

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.

import android.content.Intent import android.net.Uri import androidx.compose.foundation.clickable import androidx.compose.material3.Button import androidx.compose.material3.Checkbox import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.platform.LocalContext
@Composable fun NameInput() { var name by rememberSaveable { mutableStateOf("") } Column(modifier = Modifier.padding(16.dp)) { OutlinedTextField( value = name, onValueChange = { name = it }, label = { Text("Your name") } ) Text(if (name.isBlank()) "Type your name" else "Hello, $name!") } }
  • OutlinedTextField is the Material 3 variant with an outline, and TextField the 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.
  • rememberSaveable keeps the typed text across rotation (Chapter 2).
  • The docs describe a newer TextFieldState-based text field replacing the value and onValueChange style 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:

KindWhat you nameExample
ExplicitThe exact Activity class to startOpen your own DetailActivity
ImplicitAn action and let Android find an app that can do itOpen 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):

@Composable fun OpenWebsiteButton() { val context = LocalContext.current Button(onClick = { val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://developer.android.com")) context.startActivity(intent) }) { Text("Open website") } }

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

// In the first screen val context = LocalContext.current Button(onClick = { val intent = Intent(context, DetailActivity::class.java).putExtra("name", name) context.startActivity(intent) }) { Text("Show details") } // The second Activity class DetailActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val name = intent.getStringExtra("name") ?: "nobody" setContent { ComposeHelloTheme { Text("Hello, $name!", modifier = Modifier.padding(16.dp)) } } } }
<!-- AndroidManifest.xml: inside <application> --> <activity android:name=".DetailActivity" />
Intents or navigation?
Opening a second Activity is one way to show another screen, but in a Compose app you normally keep one Activity and switch between composable screens with Navigation Compose (Chapter 7). Intents remain the right tool for reaching outside your app (a browser, a dialler, a share sheet) and for the system starting your app. Treat the second Activity here as an exercise in how Intents carry data.

Compared with the XML Way

Views (Android Development Fundamentals)Compose
React to a tapbutton.setOnClickListener { } after findViewById or view bindingonClick = { } or Modifier.clickable { }
Read typed texteditText.text.toString()The state that onValueChange updates
Start another ActivitystartActivity(Intent(...))The same, via LocalContext.current
Read extrasintent.getStringExtra(...)The same
Compared with the Classic UI course
Android Development Fundamentals 4 teaches 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

Exercise 1

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.

๐Ÿ“„ View solution
Exercise 2

Add a Checkbox labelled "I agree" and a Button that is enabled only when the box is ticked. (Hint: Button has an enabled parameter.)

๐Ÿ“„ View solution
Exercise 3

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.

๐Ÿ“„ View solution
Not Yet Verified
Checked in the docs (one web search, summaries only): the 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).
Coming up
Chapter 6 covers lists: LazyColumn, items and keys, item composables, click handling in a list and LazyRow, the Compose replacement for RecyclerView.