// TouchPoint Example // Source code file: MainActivity.kt // Draw a circle when a user touches (clicks) // on the Canvas element. Passing Unit as a // key to Modifier.pointerInput prevents the // gesture detector from resetting across // standard recompositions. // Reference for example: Google AI Overview // using the search key "jetpack compose // detect a tap gesture draw point" package it372.ssmith.touchpoints2 import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.Canvas import androidx.compose.foundation.background import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { TapToDrawPointScreen( ) } } } @Composable fun TapToDrawPointScreen() { // Stores the coordinate of the last tapped position var tapOffset by remember { mutableStateOf(null) } Column(modifier = Modifier .background(Color(0xFFC0C0C0)) .padding(all = 30.dp)) { Canvas( modifier = Modifier .fillMaxSize() .background(Color.White) .pointerInput(Unit) { // Detects individual tap events on the screen detectTapGestures( onTap = { tapOffset = it } ) } ) { // Draw the point only if the user has tapped // the screen tapOffset?.let { offset -> drawCircle( color = Color.Blue, radius = 75f, center = offset ) } } } } @Preview(showBackground = true) @Composable fun MainLayoutPreview() { TapToDrawPointScreen( ) }