# MotionEventPredictor

Artifact: [androidx.input:input-motionprediction](https://developer.android.com/jetpack/androidx/releases/input) [View Source](https://cs.android.com/search?q=file:androidx/input/motionprediction/MotionEventPredictor.java+class:androidx.input.motionprediction.MotionEventPredictor) Added in [1.0.1](https://developer.android.com/jetpack/androidx/releases/input#1.0.1)

*** ** * ** ***


```
interface MotionEventPredictor
```

<br />

*** ** * ** ***

There is a gap between the time a user touches the screen and that information is reported to the app; a motion predictor is a utility that provides predicted `https://developer.android.com/reference/android/view/MotionEvent.html` based on the previously received ones. Obtain a new predictor instance using `https://developer.android.com/reference/kotlin/androidx/input/motionprediction/MotionEventPredictor#newInstance(android.view.View)`; put the motion events you receive into it with `https://developer.android.com/reference/kotlin/androidx/input/motionprediction/MotionEventPredictor#record(android.view.MotionEvent)`, and call `https://developer.android.com/reference/kotlin/androidx/input/motionprediction/MotionEventPredictor#predict()` to retrieve the predicted `https://developer.android.com/reference/android/view/MotionEvent.html` that would occur at the moment the next frame is rendered on the display.

## Summary

| ### Public functions |
|---|---|
| `java-static https://developer.android.com/reference/kotlin/androidx/input/motionprediction/MotionEventPredictor` | `https://developer.android.com/reference/kotlin/androidx/input/motionprediction/MotionEventPredictor#newInstance(android.view.View)(view: https://developer.android.com/reference/android/view/View.html)` Create a new motion predictor associated to a specific `https://developer.android.com/reference/android/view/View.html`. |
| `https://developer.android.com/reference/android/view/MotionEvent.html?` | `https://developer.android.com/reference/kotlin/androidx/input/motionprediction/MotionEventPredictor#predict()()` Compute a prediction |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/input/motionprediction/MotionEventPredictor#record(android.view.MotionEvent)(event: https://developer.android.com/reference/android/view/MotionEvent.html)` Record a user's movement to the predictor. |

## Public functions

### newInstance

Added in [1.0.1](https://developer.android.com/jetpack/androidx/releases/input#1.0.1)

```
java-static fun newInstance(view: View): MotionEventPredictor
```

Create a new motion predictor associated to a specific `https://developer.android.com/reference/android/view/View.html`. For devices running Android versions before U, the predicions are provided by a library based on a Kalman filter; from Android U, a system API is available, but predictions may not be supported for all strokes (for instance, it may be limited to stylus events). In these cases, the Kalman filter library will be used; to determine if a \`MotionEvent\` will be handled by the system prediction, use `https://developer.android.com/reference/android/view/MotionPredictor.html#isPredictionAvailable(int, int)`.

| Parameters |
|---|---|
| `view: https://developer.android.com/reference/android/view/View.html` | the view to associated to this predictor |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/input/motionprediction/MotionEventPredictor` | the new predictor instance |

### predict

Added in [1.0.1](https://developer.android.com/jetpack/androidx/releases/input#1.0.1)

```
fun predict(): MotionEvent?
```

Compute a prediction

| Returns |
|---|---|
| `https://developer.android.com/reference/android/view/MotionEvent.html?` | the predicted `https://developer.android.com/reference/android/view/MotionEvent.html`, or null if not possible to make a prediction. |

### record

Added in [1.0.1](https://developer.android.com/jetpack/androidx/releases/input#1.0.1)

```
fun record(event: MotionEvent): Unit
```

Record a user's movement to the predictor. You should call this for every `https://developer.android.com/reference/android/view/MotionEvent.html` that is received by the associated `https://developer.android.com/reference/android/view/View.html`.

| Parameters |
|---|---|
| `event: https://developer.android.com/reference/android/view/MotionEvent.html` | the `https://developer.android.com/reference/android/view/MotionEvent.html` the associated view received and that needs to be recorded. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if an inconsistent MotionEvent stream is sent. |