Added in API level 35

ViewBehavior


class ViewBehavior
kotlin.Any
   ↳ android.view.InputDevice.ViewBehavior

Provides information on how views processing MotionEvents generated by this input device should respond to the events. Use InputManager.getInputDeviceViewBehavior(int) to get an instance of the view behavior for an input device.

See an example below how a View can use this class to determine and apply the scrolling behavior for a generic MotionEvent.

<code>public boolean onGenericMotionEvent(MotionEvent event) {
          InputManager manager = context.getSystemService(InputManager.class);
          ViewBehavior viewBehavior = manager.getInputDeviceViewBehavior(event.getDeviceId());
          // Assume a helper function that tells us which axis to use for scrolling purpose.
          int axis = getScrollAxisForGenericMotionEvent(event);
          int source = event.getSource();
 
          boolean shouldSmoothScroll =
                  viewBehavior != null &amp;&amp; viewBehavior.shouldSmoothScroll(axis, source);
          // Proceed to running the scrolling logic...
      }
  </code>

Summary

Public methods
Int

Returns the primary directional motion axis for the input device, described as one of the MotionEvent axes.

Boolean

Whether the input device has a primary directional motion axis.

Boolean
shouldSmoothScroll(axis: Int, source: Int)

Returns whether a view should smooth scroll when scrolling due to a MotionEvent generated by the input device.

Public methods

getPrimaryDirectionalMotionAxis

fun getPrimaryDirectionalMotionAxis(): Int

Returns the primary directional motion axis for the input device, described as one of the MotionEvent axes.

When specified, the primary directional motion axis is the axis that should be preferred for gesture handling in one dimension, like flings or swipes, even if there are other axes available. For example, a touchpad with a Y axis that is physically much larger from the X axis can specify that the Y axis is the primary directional motion axis, and therefore gestures should be interpreted along the Y axis, even though an X axis also exists.

If the input device does not have a primary directional motion axis, this method will throw an IllegalStateException, so this method should only be called if hasPrimaryDirectionalMotionAxis() returns true.

Return
Int the primary directional motion axis given as one of the android.view.MotionEvent.Axis constants.
Value is one of the following:
Exceptions
java.lang.IllegalStateException if the input device does not have a primary directional motion axis (i.e., hasPrimaryDirectionalMotionAxis() returns false).

hasPrimaryDirectionalMotionAxis

fun hasPrimaryDirectionalMotionAxis(): Boolean

Whether the input device has a primary directional motion axis.

Return
Boolean true if the input device has a primary directional motion axis, or false otherwise.

shouldSmoothScroll

Added in API level 35
fun shouldSmoothScroll(
    axis: Int,
    source: Int
): Boolean

Returns whether a view should smooth scroll when scrolling due to a MotionEvent generated by the input device.

Smooth scroll in this case refers to a scroll that animates the transition between the starting and ending positions of the scroll. When this method returns true, views should try to animate a scroll generated by this device at the given axis and with the given source to produce a good scroll user experience. If this method returns false, animating scrolls is not necessary.

If the input device does not have a MotionRange with the provided axis and source, this method returns false.

Parameters
axis Int: the MotionEvent axis whose value is used to get the scroll extent.
source Int: the InputDevice source from which the MotionEvent that triggers the scroll came.
Return
Boolean true if smooth scrolling should be used for the scroll, or false if smooth scrolling is not necessary, or if the provided axis and source combination is not available for the input device.