Stay organized with collections
Save and categorize content based on your preferences.
ViewTranslationCallback
public
interface
ViewTranslationCallback
android.view.translation.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
|
onClearTranslation(View view)
Called when the translation state is no longer needed.
|
abstract
boolean
|
onHideTranslation(View view)
Called when user wants to view the original content instead of the translated content.
|
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.
|
Public methods
onClearTranslation
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.
Parameters |
view |
View : This value cannot be null . |
Returns |
boolean |
true if the View handles clearing the translation. |
onHideTranslation
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
or
View.onVirtualViewTranslationResponses
.
Parameters |
view |
View : This value cannot be null . |
Returns |
boolean |
true if the View handles hiding the translation. |
onShowTranslation
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
. This
method will not be called before View.onViewTranslationResponse
or
View.onVirtualViewTranslationResponses
.
NOTE: It is possible the user changes text that causes a new
ViewTranslationResponse
returns to show the new translation. If you cache the
ViewTranslationResponse
here, you should remember to keep the cached value up
to date.
NOTE: For TextView implementation, ContentCaptureSession.notifyViewTextChanged
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
and later,
the implementation must be able to handle a selectable TextView
(i.e., TextView.isTextSelectable()
returns true
. The default
callback implementation for TextView uses a 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
for how to get the translated information.
Parameters |
view |
View : This value cannot be null . |
Returns |
boolean |
true if the View handles showing 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.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# ViewTranslationCallback\n\nAdded in [API level 31](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\nViewTranslationCallback\n=======================\n\n*** ** * ** ***\n\n[Kotlin](/reference/kotlin/android/view/translation/ViewTranslationCallback \"View this page in Kotlin\") \\|Java\n\n\n`\npublic\n\n\ninterface\nViewTranslationCallback\n`\n\n\n`\n\n\n`\n\n|--------------------------------------------------|\n| android.view.translation.ViewTranslationCallback |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\n\u003cbr /\u003e\n\nCallback for handling the translated information show or hide in the [View](/reference/android/view/View).\n\nWhen the platform intelligence starts translation of an app's ui, the system will call\n[View.dispatchCreateViewTranslationRequest](/reference/android/view/View#dispatchCreateViewTranslationRequest(java.util.Map\u003candroid.view.autofill.AutofillId,long[]\u003e,%20int[],%20android.view.translation.TranslationCapability,%20java.util.List\u003candroid.view.translation.ViewTranslationRequest\u003e)) to collect the [ViewTranslationRequest](/reference/android/view/translation/ViewTranslationRequest)s\nfor translation purpose by traversing the hierarchy then send to translation service. After\nreceiving the [ViewTranslationResponse](/reference/android/view/translation/ViewTranslationResponse), the system will call\n[ViewTranslationCallback.onShowTranslation(View)](/reference/android/view/translation/ViewTranslationCallback#onShowTranslation(android.view.View)) to show the translated information for\nthe [View](/reference/android/view/View).\n\nSummary\n-------\n\n| ### Public methods ||\n|---------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` abstract boolean` | ` `[onClearTranslation](/reference/android/view/translation/ViewTranslationCallback#onClearTranslation(android.view.View))`(`[View](/reference/android/view/View)` view) ` Called when the translation state is no longer needed. |\n| ` abstract boolean` | ` `[onHideTranslation](/reference/android/view/translation/ViewTranslationCallback#onHideTranslation(android.view.View))`(`[View](/reference/android/view/View)` view) ` Called when user wants to view the original content instead of the translated content. |\n| ` abstract boolean` | ` `[onShowTranslation](/reference/android/view/translation/ViewTranslationCallback#onShowTranslation(android.view.View))`(`[View](/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. |\n\nPublic methods\n--------------\n\n### onClearTranslation\n\nAdded in [API level 31](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract boolean onClearTranslation (View view)\n```\n\nCalled when the translation state is no longer needed. It should restore the original content\nand clear all saved states.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|--------|---------------------------------------------|\n| `view` | `View`: This value cannot be `null`. \u003cbr /\u003e |\n\n| Returns ||\n|-----------|-------------------------------------------------------------|\n| `boolean` | `true` if the View handles clearing the translation. \u003cbr /\u003e |\n\n### onHideTranslation\n\nAdded in [API level 31](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract boolean onHideTranslation (View view)\n```\n\nCalled when user wants to view the original content instead of the translated content. This\nmethod will not be called before [View.onViewTranslationResponse](/reference/android/view/View#onViewTranslationResponse(android.view.translation.ViewTranslationResponse)) or\n[View.onVirtualViewTranslationResponses](/reference/android/view/View#onVirtualViewTranslationResponses(android.util.LongSparseArray\u003candroid.view.translation.ViewTranslationResponse\u003e)).\n\n\u003cbr /\u003e\n\n| Parameters ||\n|--------|---------------------------------------------|\n| `view` | `View`: This value cannot be `null`. \u003cbr /\u003e |\n\n| Returns ||\n|-----------|-----------------------------------------------------------|\n| `boolean` | `true` if the View handles hiding the translation. \u003cbr /\u003e |\n\n### onShowTranslation\n\nAdded in [API level 31](/guide/topics/manifest/uses-sdk-element#ApiLevels) \n\n```\npublic abstract boolean onShowTranslation (View view)\n```\n\nCalled when the translated text is ready to show or if the user has requested to reshow the\ntranslated content after hiding it.\n\n\nThe translated content can be obtained from [View.getViewTranslationResponse](/reference/android/view/View#getViewTranslationResponse()). This\nmethod will not be called before [View.onViewTranslationResponse](/reference/android/view/View#onViewTranslationResponse(android.view.translation.ViewTranslationResponse)) or\n[View.onVirtualViewTranslationResponses](/reference/android/view/View#onVirtualViewTranslationResponses(android.util.LongSparseArray\u003candroid.view.translation.ViewTranslationResponse\u003e)).\n\nNOTE: It is possible the user changes text that causes a new\n[ViewTranslationResponse](/reference/android/view/translation/ViewTranslationResponse) returns to show the new translation. If you cache the\n[ViewTranslationResponse](/reference/android/view/translation/ViewTranslationResponse) here, you should remember to keep the cached value up\nto date.\n\nNOTE: For TextView implementation, [ContentCaptureSession.notifyViewTextChanged](/reference/android/view/contentcapture/ContentCaptureSession#notifyViewTextChanged(android.view.autofill.AutofillId,%20java.lang.CharSequence))\nshouldn't be called with the translated text, simply calling setText() here will trigger the\nmethod. You should either override `View#onProvideContentCaptureStructure()` to report\nthe original text instead of the translated text or use a different approach to display the\ntranslated text.\n\nNOTE: In Android version [Build.VERSION_CODES.TIRAMISU](/reference/android/os/Build.VERSION_CODES#TIRAMISU) and later,\nthe implementation must be able to handle a selectable [TextView](/reference/android/widget/TextView)\n(i.e., [TextView.isTextSelectable()](/reference/android/widget/TextView#isTextSelectable()) returns `true`. The default\ncallback implementation for TextView uses a [TransformationMethod](/reference/android/text/method/TransformationMethod)\nto show the translated text, which will cause a crash when the translated text is selected.\nTherefore, the default callback temporarily makes the TextView non-selectable while the\ntranslation text is shown. This is one approach for handling selectable TextViews a\nTransformationMethod is used.\n\nSee [View.onViewTranslationResponse](/reference/android/view/View#onViewTranslationResponse(android.view.translation.ViewTranslationResponse)) for how to get the translated information.\n\n\u003cbr /\u003e\n\n| Parameters ||\n|--------|---------------------------------------------|\n| `view` | `View`: This value cannot be `null`. \u003cbr /\u003e |\n\n| Returns ||\n|-----------|------------------------------------------------------------|\n| `boolean` | `true` if the View handles showing the translation. \u003cbr /\u003e |"]]