Gesture methods allow you to simulate touch interactions at specific screen coordinates. These methods use Android’s GestureDescription API to dispatch touch events.
Gesture methods require API 24 (Android 7.0) or higher. The methods will log a warning and return early on older Android versions.
The Y coordinate in pixels (vertical position from top edge)
Example:
Copy
val service = MyAccessibilityService.instance// Tap at position (500, 800)service?.simulateClick(500f, 800f)// Tap at center of screen (assuming 1080x1920 resolution)service?.simulateClick(540f, 960f)
The tap gesture has a 50ms duration. This simulates a quick tap that most apps will recognize as a click event.
val service = MyAccessibilityService.instance// Swipe from bottom to top (scroll down)service?.simulateSwipe( startX = 500f, startY = 1500f, endX = 500f, endY = 500f)// Swipe from right to left (horizontal scroll)service?.simulateSwipe( startX = 900f, startY = 800f, endX = 200f, endY = 800f)
The swipe gesture has a 500ms duration, creating a smooth scrolling motion that mimics natural user interaction.
Simulate a vertical swipe to scroll down the page.
Copy
fun simulateScrollToBottom()
Example:
Copy
val service = MyAccessibilityService.instance// Scroll down to reveal more contentservice?.simulateScrollToBottom()Thread.sleep(500) // Wait for scroll animationservice?.simulateScrollToBottom()
This method performs a swipe from Y=1200 to Y=300 at X=300, creating a downward scroll motion. The gesture duration is 700ms.
val service = MyAccessibilityService.instance// Get bounds of an element by view IDval bounds = service?.getElementBoundsByViewId("com.example.app:id/button")if (bounds != null) { // Tap at center of the element val centerX = bounds.exactCenterX() val centerY = bounds.exactCenterY() service?.simulateClick(centerX, centerY) // Or tap at top-left corner service?.simulateClick(bounds.left.toFloat(), bounds.top.toFloat())}
Create complex interactions with sequential gestures:
Copy
val service = MyAccessibilityService.instance// Open a menu with long press simulationrepeat(3) { service?.simulateClick(500f, 800f) Thread.sleep(50)}// Swipe through carouselrepeat(5) { service?.simulateSwipe( startX = 800f, startY = 600f, endX = 200f, endY = 600f ) Thread.sleep(1000) // Wait for animation}
Pinch and zoom gestures require multi-touch support and are not directly implemented. You would need to create custom GestureDescription with multiple strokes.
Example concept (not implemented in current API):
Copy
// This is pseudocode - not available in current implementationfun simulatePinchZoom(centerX: Float, centerY: Float, scale: Float) { val gestureBuilder = GestureDescription.Builder() // First finger val path1 = Path().apply { moveTo(centerX - 100, centerY) lineTo(centerX - 100 * scale, centerY) } gestureBuilder.addStroke(StrokeDescription(path1, 0, 500)) // Second finger val path2 = Path().apply { moveTo(centerX + 100, centerY) lineTo(centerX + 100 * scale, centerY) } gestureBuilder.addStroke(StrokeDescription(path2, 0, 500)) dispatchGesture(gestureBuilder.build(), null, null)}
val service = MyAccessibilityService.instance// Click buttonservice?.simulateClick(500f, 800f)// Wait for page transitionThread.sleep(1000)// Check if new page loadedif (service?.isTextPresentOnScreen("Welcome") == true) { println("Navigation successful")}// Type text slowly to trigger autocompleteservice?.simulateTypeInFirstEditableField("user")Thread.sleep(500)service?.simulateTypeInFirstEditableField("username@example.com")
val service = MyAccessibilityService.instance// Load more items by scrollingrepeat(10) { service?.simulateScrollToBottom() Thread.sleep(1500) // Wait for content to load if (service?.isTextPresentOnScreen("No more items") == true) { break }}
// Swipe from top down to trigger refreshservice?.simulateSwipe( startX = 500f, startY = 200f, endX = 500f, endY = 1000f)Thread.sleep(2000) // Wait for refresh
Enable touch visualization to debug gesture coordinates:
Copy
# Enable show touches in Developer Optionsadb shell settings put system show_touches 1# Enable pointer locationadb shell settings put system pointer_location 1# Disable after debuggingadb shell settings put system show_touches 0adb shell settings put system pointer_location 0
Log gesture coordinates:
Copy
fun simulateClick(x: Float, y: Float) { Log.d("Gesture", "Tapping at ($x, $y)") // ... implementation}