Skip to content

Most visited

Recently visited



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.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)