# ViewTranslationCallback

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

# ViewTranslationCallback

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/android/view/translation/ViewTranslationCallback "View this page in Kotlin")\|Java

`
public


interface
ViewTranslationCallback
`  
`


`

|--------------------------------------------------|
| android.view.translation.ViewTranslationCallback |

<br />

*** ** * ** ***

<br />

Callback for handling the translated information show or hide in the[View](https://developer.android.com/reference/android/view/View).

When the platform intelligence starts translation of an app's ui, the system will call[View.dispatchCreateViewTranslationRequest](https://developer.android.com/reference/android/view/View#dispatchCreateViewTranslationRequest(java.util.Map<android.view.autofill.AutofillId,long[]>,%20int[],%20android.view.translation.TranslationCapability,%20java.util.List<android.view.translation.ViewTranslationRequest>))to collect the[ViewTranslationRequest](https://developer.android.com/reference/android/view/translation/ViewTranslationRequest)s for translation purpose by traversing the hierarchy then send to translation service. After receiving the[ViewTranslationResponse](https://developer.android.com/reference/android/view/translation/ViewTranslationResponse), the system will call[ViewTranslationCallback.onShowTranslation(View)](https://developer.android.com/reference/android/view/translation/ViewTranslationCallback#onShowTranslation(android.view.View))to show the translated information for the[View](https://developer.android.com/reference/android/view/View).

## Summary

|                                                                                                                                                                                   ### Public methods                                                                                                                                                                                    ||
|---------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` abstract boolean` | ` `[onClearTranslation](https://developer.android.com/reference/android/view/translation/ViewTranslationCallback#onClearTranslation(android.view.View))`(`[View](https://developer.android.com/reference/android/view/View)` view) ` Called when the translation state is no longer needed.                                                                        |
| ` abstract boolean` | ` `[onHideTranslation](https://developer.android.com/reference/android/view/translation/ViewTranslationCallback#onHideTranslation(android.view.View))`(`[View](https://developer.android.com/reference/android/view/View)` view) ` Called when user wants to view the original content instead of the translated content.                                          |
| ` abstract boolean` | ` `[onShowTranslation](https://developer.android.com/reference/android/view/translation/ViewTranslationCallback#onShowTranslation(android.view.View))`(`[View](https://developer.android.com/reference/android/view/View)` view) ` Called when the translated text is ready to show or if the user has requested to reshow the translated content after hiding it. |

## Public methods

### onClearTranslation

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

```
public abstract boolean onClearTranslation (View view)
```

Called when the translation state is no longer needed. It should restore the original content and clear all saved states.

<br />

|                     Parameters                     ||
|--------|--------------------------------------------|
| `view` | `View`: This value cannot be`null`. <br /> |

|                                Returns                                ||
|-----------|------------------------------------------------------------|
| `boolean` | `true`if the View handles clearing the translation. <br /> |

### onHideTranslation

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

```
public abstract boolean onHideTranslation (View view)
```

Called when user wants to view the original content instead of the translated content. This method will not be called before[View.onViewTranslationResponse](https://developer.android.com/reference/android/view/View#onViewTranslationResponse(android.view.translation.ViewTranslationResponse))or[View.onVirtualViewTranslationResponses](https://developer.android.com/reference/android/view/View#onVirtualViewTranslationResponses(android.util.LongSparseArray<android.view.translation.ViewTranslationResponse>)).

<br />

|                     Parameters                     ||
|--------|--------------------------------------------|
| `view` | `View`: This value cannot be`null`. <br /> |

|                               Returns                               ||
|-----------|----------------------------------------------------------|
| `boolean` | `true`if the View handles hiding the translation. <br /> |

### onShowTranslation

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

```
public abstract boolean onShowTranslation (View view)
```

Called when the translated text is ready to show or if the user has requested to reshow the translated content after hiding it.

The translated content can be obtained from[View.getViewTranslationResponse](https://developer.android.com/reference/android/view/View#getViewTranslationResponse()). This method will not be called before[View.onViewTranslationResponse](https://developer.android.com/reference/android/view/View#onViewTranslationResponse(android.view.translation.ViewTranslationResponse))or[View.onVirtualViewTranslationResponses](https://developer.android.com/reference/android/view/View#onVirtualViewTranslationResponses(android.util.LongSparseArray<android.view.translation.ViewTranslationResponse>)).

NOTE: It is possible the user changes text that causes a new[ViewTranslationResponse](https://developer.android.com/reference/android/view/translation/ViewTranslationResponse)returns to show the new translation. If you cache the[ViewTranslationResponse](https://developer.android.com/reference/android/view/translation/ViewTranslationResponse)here, you should remember to keep the cached value up to date.

NOTE: For TextView implementation,[ContentCaptureSession.notifyViewTextChanged](https://developer.android.com/reference/android/view/contentcapture/ContentCaptureSession#notifyViewTextChanged(android.view.autofill.AutofillId,%20java.lang.CharSequence))shouldn't be called with the translated text, simply calling setText() here will trigger the method. You should either override`View#onProvideContentCaptureStructure()`to report the original text instead of the translated text or use a different approach to display the translated text.

NOTE: In Android version[Build.VERSION_CODES.TIRAMISU](https://developer.android.com/reference/android/os/Build.VERSION_CODES#TIRAMISU)and later, the implementation must be able to handle a selectable[TextView](https://developer.android.com/reference/android/widget/TextView)(i.e.,[TextView.isTextSelectable()](https://developer.android.com/reference/android/widget/TextView#isTextSelectable())returns`true`. The default callback implementation for TextView uses a[TransformationMethod](https://developer.android.com/reference/android/text/method/TransformationMethod)to show the translated text, which will cause a crash when the translated text is selected. Therefore, the default callback temporarily makes the TextView non-selectable while the translation text is shown. This is one approach for handling selectable TextViews a TransformationMethod is used. See[View.onViewTranslationResponse](https://developer.android.com/reference/android/view/View#onViewTranslationResponse(android.view.translation.ViewTranslationResponse))for how to get the translated information.

<br />

|                     Parameters                     ||
|--------|--------------------------------------------|
| `view` | `View`: This value cannot be`null`. <br /> |

|                               Returns                                ||
|-----------|-----------------------------------------------------------|
| `boolean` | `true`if the View handles showing the translation. <br /> |