Stay organized with collections
Save and categorize content based on your preferences.
ViewTranslationCallback
interface ViewTranslationCallback
Callback for handling the translated information show or hide in the View
.
When the platform intelligence starts translation of an app's ui, the system will call View.dispatchCreateViewTranslationRequest
to collect the ViewTranslationRequest
s for translation purpose by traversing the hierarchy then send to translation service. After receiving the ViewTranslationResponse
, the system will call ViewTranslationCallback.onShowTranslation(View)
to show the translated information for the View
.
Summary
Public methods |
abstract Boolean |
Called when the translation state is no longer needed.
|
abstract Boolean |
Called when user wants to view the original content instead of the translated content.
|
abstract Boolean |
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
abstract fun onClearTranslation(view: View): Boolean
Called when the translation state is no longer needed. It should restore the original content and clear all saved states.
Parameters |
view |
View: This value cannot be null . |
Return |
Boolean |
true if the View handles clearing the translation. |
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,["# ViewTranslationCallback\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nViewTranslationCallback\n=======================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/view/translation/ViewTranslationCallback \"View this page in Java\") \n\n```\ninterface ViewTranslationCallback\n```\n\n|-------------------------------------------------------|\n| [android.view.translation.ViewTranslationCallback](#) |\n\nCallback for handling the translated information show or hide in the [View](../View.html#).\n\nWhen the platform intelligence starts translation of an app's ui, the system will call [View.dispatchCreateViewTranslationRequest](../View.html#dispatchCreateViewTranslationRequest(kotlin.collections.MutableMap,%20kotlin.IntArray,%20android.view.translation.TranslationCapability,%20kotlin.collections.MutableList)) to collect the [ViewTranslationRequest](/reference/kotlin/android/view/translation/ViewTranslationRequest)s for translation purpose by traversing the hierarchy then send to translation service. After receiving the [ViewTranslationResponse](/reference/kotlin/android/view/translation/ViewTranslationResponse), the system will call [ViewTranslationCallback.onShowTranslation(View)](#onShowTranslation(android.view.View)) to show the translated information for the [View](../View.html#).\n\nSummary\n-------\n\n| Public methods ||\n|---------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [onClearTranslation](#onClearTranslation(android.view.View))`(`view:` `[View](../View.html#)`)` Called when the translation state is no longer needed. |\n| abstract [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [onHideTranslation](#onHideTranslation(android.view.View))`(`view:` `[View](../View.html#)`)` Called when user wants to view the original content instead of the translated content. |\n| abstract [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | [onShowTranslation](#onShowTranslation(android.view.View))`(`view:` `[View](../View.html#)`)` Called when the translated text is ready to show or if the user has requested to reshow the translated content after hiding it. |\n\nPublic methods\n--------------\n\n### onClearTranslation\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onClearTranslation(view: View): Boolean\n```\n\nCalled when the translation state is no longer needed. It should restore the original content and clear all saved states.\n\n| Parameters ||\n|--------|-----------------------------------------------------|\n| `view` | [View](../View.html#): This value cannot be `null`. |\n\n| Return ||\n|------------------------------------------------------------------------------------|------------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `true` if the View handles clearing the translation. |\n\n### onHideTranslation\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onHideTranslation(view: View): Boolean\n```\n\nCalled when user wants to view the original content instead of the translated content. This method will not be called before [View.onViewTranslationResponse](../View.html#onViewTranslationResponse(android.view.translation.ViewTranslationResponse)) or [View.onVirtualViewTranslationResponses](../View.html#onVirtualViewTranslationResponses(android.util.LongSparseArray)).\n\n| Parameters ||\n|--------|-----------------------------------------------------|\n| `view` | [View](../View.html#): This value cannot be `null`. |\n\n| Return ||\n|------------------------------------------------------------------------------------|----------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `true` if the View handles hiding the translation. |\n\n### onShowTranslation\n\nAdded in [API level 31](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nabstract fun onShowTranslation(view: View): Boolean\n```\n\nCalled when the translated text is ready to show or if the user has requested to reshow the translated content after hiding it.\n\nThe translated content can be obtained from [View.getViewTranslationResponse](../View.html#getViewTranslationResponse()). This method will not be called before [View.onViewTranslationResponse](../View.html#onViewTranslationResponse(android.view.translation.ViewTranslationResponse)) or [View.onVirtualViewTranslationResponses](../View.html#onVirtualViewTranslationResponses(android.util.LongSparseArray)).\n\nNOTE: It is possible the user changes text that causes a new [ViewTranslationResponse](/reference/kotlin/android/view/translation/ViewTranslationResponse) returns to show the new translation. If you cache the [ViewTranslationResponse](/reference/kotlin/android/view/translation/ViewTranslationResponse) here, you should remember to keep the cached value up to date.\n\nNOTE: For TextView implementation, [ContentCaptureSession.notifyViewTextChanged](../contentcapture/ContentCaptureSession.html#notifyViewTextChanged(android.view.autofill.AutofillId,%20kotlin.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.\n\nNOTE: In Android version [android.os.Build.VERSION_CODES#TIRAMISU](../../os/Build.VERSION_CODES.html#TIRAMISU:kotlin.Int) and later, the implementation must be able to handle a selectable [android.widget.TextView](../../widget/TextView.html#) (i.e., [android.widget.TextView#isTextSelectable()](../../widget/TextView.html#isTextSelectable()) returns `true`. The default callback implementation for TextView uses a [android.text.method.TransformationMethod](../../text/method/TransformationMethod.html#) 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](../View.html#onViewTranslationResponse(android.view.translation.ViewTranslationResponse)) for how to get the translated information.\n\n| Parameters ||\n|--------|-----------------------------------------------------|\n| `view` | [View](../View.html#): This value cannot be `null`. |\n\n| Return ||\n|------------------------------------------------------------------------------------|-----------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/index.html) | `true` if the View handles showing the translation. |"]]