Some Wear OS devices contain a physical rotating side button. When the user turns the button, it scrolls your app's current view up or down. This type of input is called rotary input.
Note: This guide refers primarily to handling rotary input using View-based UIs. For more information on handling rotary input using Compose for Wear OS, see Rotary input on Compose.
Many scrollable containers, like
ScrollView
,
ListView
,
HorizontalScrollView
,
and WearableRecyclerView
,
support rotary input if they have focus without requiring any Wear
OS-specific code.
Having focus is an important prerequisite, because on Android 9 (API level
28) and higher, views don't implicitly receive focus.
Focus best practices
To respond to rotary input events, a scrollable container must have focus.
Rotary input events don't bubble up the view
hierarchy. If there is no focused view, or if the focused view returns false
from
View.onGenericMotionEvent()
,
then the event is sent to
Activity.onGenericMotionEvent()
.
The following are best practices around responding to rotary input events:
- Bear in mind that, by default, launching an activity or even tapping on a
view does not give it focus, even if it is
focusable. To give your view focus, the view must use the
<requestFocus />
tag or manually callView.requestFocus()
. - Mark custom scrollable views as focusable using both
android:focusable="true"
andandroid:focusableInTouchMode="true"
. - If your scrollable view is attached after
Activity.onCreate()
—for example, waiting for a network request to finish before building your UI, callrequestFocus()
after attaching it. - If your scrollable view is initially
INVISIBLE
orGONE
, callrequestFocus()
when you set it toVISIBLE
. - If your activity contains multiple scrollable views, choose one to focus using the
<requestFocus />
tag. Nested scrolling is not supported with the rotating side button. - If your UI contains some other view that takes focus when the user
interacts with it—for example,
an
InputText
, give the user a way to restore focus to the scrollable view if it loses focus by listening for taps on the scrollable view and callingrequestFocus()
in response.
Custom rotating behavior
If your scrollable view doesn't natively support rotary input scrolling, or if you want to use your rotary input for something other than scrolling—such as to zoom in and out or to turn dials—you can handle the scroll events yourself. Remember to make sure your view gains focus, otherwise the events will not come through.
The following code snippet shows how to use MotionEvent
,
InputDeviceCompat
,
and ViewConfigurationCompat
to add custom scrolling to your view:
Kotlin
myView.setOnGenericMotionListener { v, ev -> if (ev.action == MotionEvent.ACTION_SCROLL && ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER) ) { // Don't forget the negation here val delta = -ev.getAxisValue(MotionEventCompat.AXIS_SCROLL) * ViewConfigurationCompat.getScaledVerticalScrollFactor( ViewConfiguration.get(context), context ) // Swap these axes to scroll horizontally instead v.scrollBy(0, delta.roundToInt()) true } else { false } }
Java
myView.setOnGenericMotionListener(new View.OnGenericMotionListener() { @Override public boolean onGenericMotion(View v, MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_SCROLL && ev.isFromSource(InputDeviceCompat.SOURCE_ROTARY_ENCODER) ) { // Don't forget the negation here float delta = -ev.getAxisValue(MotionEventCompat.AXIS_SCROLL) * ViewConfigurationCompat.getScaledVerticalScrollFactor( ViewConfiguration.get(context), context ); // Swap these axes to scroll horizontally instead v.scrollBy(0, Math.round(delta)); return true; } return false; } });
Test using an emulator
Use the Android Emulator to simulate rotary input scrolling on a Wear device. Launch your Wear app on the emulator to run your project or drag an APK file onto the emulator to install it.
To test the rotary input on the emulator:
- From the SDK manager, use the SDK tools tab to get Android Emulator 26.0.3 or higher.
- In Android Studio, select Tools > Android > AVD Manager. Create a new Wear device with API 25 or higher.
- Run the emulator from Android Studio.
- Click the three-dot overflow menu at the bottom of the emulator toolbar. Click the Rotary input tab in the new window to open the rotary input interface and try rotary input scrolling.
The following video shows rotary input in the emulator: