Stay organized with collections
Save and categorize content based on your preferences.
ViewBehavior
class ViewBehavior
Provides information on how views processing MotionEvent
s 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 && viewBehavior.shouldSmoothScroll(axis, source);
// Proceed to running the scrolling logic...
}
</code>
Summary
Public methods |
Boolean |
Returns whether a view should smooth scroll when scrolling due to a MotionEvent generated by the input device.
|
Public methods
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
.
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. |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# InputDevice.ViewBehavior\n\nAdded in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nViewBehavior\n============\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/view/InputDevice.ViewBehavior \"View this page in Java\") \n\n```\nclass ViewBehavior\n```\n\n|---|--------------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.view.InputDevice.ViewBehavior](#) |\n\nProvides information on how views processing [MotionEvent](/reference/kotlin/android/view/MotionEvent)s generated by this input device should respond to the events. Use [InputManager.getInputDeviceViewBehavior(int)](../hardware/input/InputManager.html#getInputDeviceViewBehavior(kotlin.Int)) to get an instance of the view behavior for an input device.\n\nSee an example below how a [View](/reference/kotlin/android/view/View) can use this class to determine and apply the scrolling behavior for a generic [MotionEvent](/reference/kotlin/android/view/MotionEvent). \n\n```kotlin\n\u003ccode\u003epublic boolean onGenericMotionEvent(MotionEvent event) {\n InputManager manager = context.getSystemService(InputManager.class);\n ViewBehavior viewBehavior = manager.getInputDeviceViewBehavior(event.getDeviceId());\n // Assume a helper function that tells us which axis to use for scrolling purpose.\n int axis = getScrollAxisForGenericMotionEvent(event);\n int source = event.getSource();\n \n boolean shouldSmoothScroll =\n viewBehavior != null && viewBehavior.shouldSmoothScroll(axis, source);\n // Proceed to running the scrolling logic...\n }\n \u003c/code\u003e\n```\n\nSummary\n-------\n\n| Public methods ||\n|------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [shouldSmoothScroll](#shouldSmoothScroll(kotlin.Int,%20kotlin.Int))`(`axis:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`, `source:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Returns whether a view should smooth scroll when scrolling due to a [MotionEvent](/reference/kotlin/android/view/MotionEvent) generated by the input device. |\n\nPublic methods\n--------------\n\n### shouldSmoothScroll\n\nAdded in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nfun shouldSmoothScroll(\n axis: Int, \n source: Int\n): Boolean\n```\n\nReturns whether a view should smooth scroll when scrolling due to a [MotionEvent](/reference/kotlin/android/view/MotionEvent) generated by the input device.\n\nSmooth 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.\n\nIf the input device does not have a [MotionRange](/reference/kotlin/android/view/InputDevice.MotionRange) with the provided axis and source, this method returns `false`.\n\n| Parameters ||\n|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `axis` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): the [MotionEvent](/reference/kotlin/android/view/MotionEvent) axis whose value is used to get the scroll extent. |\n| `source` | [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html): the [InputDevice](/reference/kotlin/android/view/InputDevice) source from which the [MotionEvent](/reference/kotlin/android/view/MotionEvent) that triggers the scroll came. |\n\n| Return ||\n|------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Boolean](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. |"]]