Join us for ⁠#Android11: The Beta Launch Show on June 3!


public class RotaryEncoder
extends Object


Collection of helper methods for processing rotary encoder events. Most widgets already support these events, but custom behavior can be added if desired. To do so, either override onGenericMotionEvent(MotionEvent) or specify a custom handler via setOnGenericMotionListener(OnGenericMotionListener).

 public class MyCustomView extends View {
   // ...

   public boolean onGenericMotionEvent(MotionEvent ev) {
     if (ev.getAction() == MotionEvent.ACTION_SCROLL
         && RotaryEncoder.isFromRotaryEncoder(ev)) {
       // Note that we negate the delta value here in order to get the right scroll direction.
       float delta = -RotaryEncoder.getRotaryAxisValue(ev)
           * RotaryEncoder.getScaledScrollFactor(getContext());
       scrollBy(0, Math.round(delta));
       return true;
     return super.onGenericMotionEvent(ev);

   // ...


Public methods

static float getRotaryAxisValue(MotionEvent ev)
static float getScaledScrollFactor(Context context)
static boolean isFromRotaryEncoder(MotionEvent ev)

Checks whether a MotionEvent was generated by a rotary encoder.

Inherited methods

From class java.lang.Object

Public methods


float getRotaryAxisValue (MotionEvent ev)

ev MotionEvent

float The raw axis value (i.e. the amount the encoder was rotated) if the passed event is from a rotary encoder, and zero otherwise. This value should be multiplied against getScaledScrollFactor(Context) to determine the distance to scroll in pixels.


float getScaledScrollFactor (Context context)

context Context

float The scaling factor that rotary axis values should be multiplied against to determine how many pixels to scroll.


boolean isFromRotaryEncoder (MotionEvent ev)

Checks whether a MotionEvent was generated by a rotary encoder. Does not check the action type of the event; you should do this as well, e.g. event.getAction() == MotionEvent.ACTION_SCROLL. At the moment, rotary encoders only generate ACTION_SCROLL events, but they may generate other types of events in the future.

ev MotionEvent

boolean True if this event was generated by a rotary encoder.