Added in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# ViewBehavior

*** ** * ** ***

```
class ViewBehavior
```

|---|---|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||
| ↳ | [android.view.InputDevice.ViewBehavior](https://developer.android.com/reference/kotlin/android/view/InputDevice.ViewBehavior#) |

Provides information on how views processing `https://developer.android.com/reference/kotlin/android/view/MotionEvent`s generated by this input device should respond to the events. Use `https://developer.android.com/reference/kotlin/android/hardware/input/InputManager.html#getInputDeviceViewBehavior(kotlin.Int)` to get an instance of the view behavior for an input device.

See an example below how a `https://developer.android.com/reference/kotlin/android/view/View` can use this class to determine and apply the scrolling behavior for a generic `https://developer.android.com/reference/kotlin/android/view/MotionEvent`.

```kotlin
<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](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | `https://developer.android.com/reference/kotlin/android/view/InputDevice.ViewBehavior#getPrimaryDirectionalMotionAxis()()` Returns the primary directional motion axis for the input device, described as one of the `https://developer.android.com/reference/kotlin/android/view/MotionEvent` axes. |
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `https://developer.android.com/reference/kotlin/android/view/InputDevice.ViewBehavior#hasPrimaryDirectionalMotionAxis()()` Whether the input device has a primary directional motion axis. |
| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `https://developer.android.com/reference/kotlin/android/view/InputDevice.ViewBehavior#shouldSmoothScroll(kotlin.Int,%20kotlin.Int)(axis: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html, source: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)` Returns whether a view should smooth scroll when scrolling due to a `https://developer.android.com/reference/kotlin/android/view/MotionEvent` generated by the input device. |

## Public methods

### getPrimaryDirectionalMotionAxis

Added in [API level 37.2](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun getPrimaryDirectionalMotionAxis(): Int
```

Returns the primary directional motion axis for the input device, described as one of the `https://developer.android.com/reference/kotlin/android/view/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 `https://developer.android.com/reference/kotlin/java/lang/IllegalStateException.html#`, so this method should only be called if `https://developer.android.com/reference/kotlin/android/view/InputDevice.ViewBehavior#hasPrimaryDirectionalMotionAxis()` returns `true`.

| Return ||
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html` | the primary directional motion axis given as one of the android.view.MotionEvent.Axis constants. Value is one of the following: - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_X:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_Y:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_PRESSURE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_SIZE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_TOUCH_MAJOR:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_TOUCH_MINOR:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_TOOL_MAJOR:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_TOOL_MINOR:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_ORIENTATION:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_VSCROLL:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_HSCROLL:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_Z:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_RX:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_RY:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_RZ:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_HAT_X:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_HAT_Y:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_LTRIGGER:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_RTRIGGER:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_THROTTLE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_RUDDER:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_WHEEL:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GAS:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_BRAKE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_DISTANCE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_TILT:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_SCROLL:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_RELATIVE_X:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_RELATIVE_Y:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_1:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_2:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_3:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_4:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_5:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_6:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_7:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_8:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_9:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_10:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_11:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_12:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_13:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_14:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_15:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GENERIC_16:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GESTURE_X_OFFSET:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GESTURE_Y_OFFSET:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GESTURE_SCROLL_X_DISTANCE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GESTURE_SCROLL_Y_DISTANCE:kotlin.Int` - `https://developer.android.com/reference/kotlin/android/view/MotionEvent#AXIS_GESTURE_PINCH_SCALE_FACTOR:kotlin.Int` |

| Exceptions ||
|---|---|
| `java.lang.IllegalStateException` | if the input device does not have a primary directional motion axis (i.e., `https://developer.android.com/reference/kotlin/android/view/InputDevice.ViewBehavior#hasPrimaryDirectionalMotionAxis()` returns `false`). |

### hasPrimaryDirectionalMotionAxis

Added in [API level 37.2](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun hasPrimaryDirectionalMotionAxis(): Boolean
```

Whether the input device has a primary directional motion axis.

| Return ||
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `true` if the input device has a primary directional motion axis, or `false` otherwise. |

### shouldSmoothScroll

Added in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

```
fun shouldSmoothScroll(
    axis: Int, 
    source: Int
): Boolean
```

Returns whether a view should smooth scroll when scrolling due to a `https://developer.android.com/reference/kotlin/android/view/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 `https://developer.android.com/reference/kotlin/android/view/InputDevice.MotionRange` with the provided axis and source, this method returns `false`.

| Parameters ||
|---|---|
| `axis` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): the `https://developer.android.com/reference/kotlin/android/view/MotionEvent` axis whose value is used to get the scroll extent. |
| `source` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): the `https://developer.android.com/reference/kotlin/android/view/InputDevice` source from which the `https://developer.android.com/reference/kotlin/android/view/MotionEvent` that triggers the scroll came. |

| Return ||
|---|---|
| `https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html` | `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. |