AccessibilityDelegate
open class AccessibilityDelegate
kotlin.Any | |
↳ | android.view.View.AccessibilityDelegate |
This class represents a delegate that can be registered in a View
to enhance accessibility support via composition rather via inheritance. It is specifically targeted to widget developers that extend basic View classes i.e. classes in package android.view, that would like their applications to be backwards compatible.
A scenario in which a developer would like to use an accessibility delegate is overriding a method introduced in a later API version than the minimal API version supported by the application. For example, the method View.onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
is not available in API version 4 when the accessibility APIs were first introduced. If a developer would like their application to run on API version 4 devices (assuming all other APIs used by the application are version 4 or lower) and take advantage of this method, instead of overriding the method which would break the application's backwards compatibility, they can override the corresponding method in this delegate and register the delegate in the target View if the API version of the system is high enough, i.e. the API version is the same as or higher than the API version that introduced View.onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
.
Here is an example implementation:
<code><p> if (Build.VERSION.SDK_INT >= 14) { // If the API version is equal of higher than the version in // which onInitializeAccessibilityNodeInfo was introduced we // register a delegate with a customized implementation. View view = findViewById(R.id.view_id); view.setAccessibilityDelegate(new AccessibilityDelegate() { public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) { // Let the default implementation populate the info. super.onInitializeAccessibilityNodeInfo(host, info); // Set some other information. info.setEnabled(host.isEnabled()); } }); } </p></code>
This delegate contains methods that correspond to the accessibility methods in View. If a delegate has been specified the implementation in View hands off handling to the corresponding method in this delegate. The default implementation the delegate methods behaves exactly as the corresponding method in View for the case of no accessibility delegate been set. Hence, to customize the behavior of a View method, clients can override only the corresponding delegate method without altering the behavior of the rest accessibility related methods of the host view.
Note: On platform versions prior to API 23
, delegate methods on views in the android.widget.*
package are called before host methods. This prevents certain properties such as class name from being modified by overriding AccessibilityDelegate.onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)
, as any changes will be overwritten by the host class.
Starting in API 23
, delegate methods are called after host methods, which all properties to be modified without being overwritten by the host class.
Summary
Public constructors | |
---|---|
Public methods | |
---|---|
open Unit |
addExtraDataToAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfo, extraDataKey: String, arguments: Bundle?) Adds extra data to an |
open Boolean |
dispatchPopulateAccessibilityEvent(host: View, event: AccessibilityEvent) Dispatches an |
open AccessibilityNodeProvider? |
getAccessibilityNodeProvider(host: View) Gets the provider for managing a virtual view hierarchy rooted at this View and reported to |
open Unit |
onInitializeAccessibilityEvent(host: View, event: AccessibilityEvent) Initializes an |
open Unit |
onInitializeAccessibilityNodeInfo(host: View, info: AccessibilityNodeInfo) Initializes an |
open Unit |
onPopulateAccessibilityEvent(host: View, event: AccessibilityEvent) Gives a chance to the host View to populate the accessibility event with its text content. |
open Boolean |
onRequestSendAccessibilityEvent(host: ViewGroup, child: View, event: AccessibilityEvent) Called when a child of the host View has requested sending an |
open Boolean |
performAccessibilityAction(host: View, action: Int, args: Bundle?) Performs the specified accessibility action on the view. |
open Unit |
sendAccessibilityEvent(host: View, eventType: Int) Sends an accessibility event of the given type. |
open Unit |
sendAccessibilityEventUnchecked(host: View, event: AccessibilityEvent) Sends an accessibility event. |
Public constructors
AccessibilityDelegate
AccessibilityDelegate()
Public methods
addExtraDataToAccessibilityNodeInfo
open fun addExtraDataToAccessibilityNodeInfo(
host: View,
info: AccessibilityNodeInfo,
extraDataKey: String,
arguments: Bundle?
): Unit
Adds extra data to an AccessibilityNodeInfo
based on an explicit request for the additional data.
This method only needs to be implemented if the View offers to provide additional data.
The default implementation behaves as View.addExtraDataToAccessibilityNodeInfo(AccessibilityNodeInfo, String, Bundle)
dispatchPopulateAccessibilityEvent
open fun dispatchPopulateAccessibilityEvent(
host: View,
event: AccessibilityEvent
): Boolean
Dispatches an AccessibilityEvent
to the host View
first and then to its children for adding their text content to the event.
The default implementation behaves as View#dispatchPopulateAccessibilityEvent(AccessibilityEvent)
for the case of no accessibility delegate been set.
Parameters | |
---|---|
host |
View: The View hosting the delegate. This value cannot be null . |
event |
AccessibilityEvent: The event. This value cannot be null . |
Return | |
---|---|
Boolean |
True if the event population was completed. |
getAccessibilityNodeProvider
open fun getAccessibilityNodeProvider(host: View): AccessibilityNodeProvider?
Gets the provider for managing a virtual view hierarchy rooted at this View and reported to android.accessibilityservice.AccessibilityService
s that explore the window content.
The default implementation behaves as View#getAccessibilityNodeProvider()
for the case of no accessibility delegate been set.
Parameters | |
---|---|
host |
View: This value cannot be null . |
Return | |
---|---|
AccessibilityNodeProvider? |
The provider. This value may be null . |
onInitializeAccessibilityEvent
open fun onInitializeAccessibilityEvent(
host: View,
event: AccessibilityEvent
): Unit
Initializes an AccessibilityEvent
with information about the the host View which is the event source.
The default implementation behaves as View#onInitializeAccessibilityEvent(AccessibilityEvent)
for the case of no accessibility delegate been set.
Parameters | |
---|---|
host |
View: The View hosting the delegate. This value cannot be null . |
event |
AccessibilityEvent: The event to initialize. This value cannot be null . |
onInitializeAccessibilityNodeInfo
open fun onInitializeAccessibilityNodeInfo(
host: View,
info: AccessibilityNodeInfo
): Unit
Initializes an AccessibilityNodeInfo
with information about the host view.
The default implementation behaves as View#onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)
for the case of no accessibility delegate been set.
Parameters | |
---|---|
host |
View: The View hosting the delegate. This value cannot be null . |
info |
AccessibilityNodeInfo: The instance to initialize. This value cannot be null . |
onPopulateAccessibilityEvent
open fun onPopulateAccessibilityEvent(
host: View,
event: AccessibilityEvent
): Unit
Gives a chance to the host View to populate the accessibility event with its text content.
The default implementation behaves as View#onPopulateAccessibilityEvent(AccessibilityEvent)
for the case of no accessibility delegate been set.
Parameters | |
---|---|
host |
View: The View hosting the delegate. This value cannot be null . |
event |
AccessibilityEvent: The accessibility event which to populate. This value cannot be null . |
onRequestSendAccessibilityEvent
open fun onRequestSendAccessibilityEvent(
host: ViewGroup,
child: View,
event: AccessibilityEvent
): Boolean
Called when a child of the host View has requested sending an AccessibilityEvent
and gives an opportunity to the parent (the host) to augment the event.
The default implementation behaves as ViewGroup#onRequestSendAccessibilityEvent(View, AccessibilityEvent)
for the case of no accessibility delegate been set.
Parameters | |
---|---|
host |
ViewGroup: The View hosting the delegate. This value cannot be null . |
child |
View: The child which requests sending the event. This value cannot be null . |
event |
AccessibilityEvent: The event to be sent. This value cannot be null . |
Return | |
---|---|
Boolean |
True if the event should be sent |
performAccessibilityAction
open fun performAccessibilityAction(
host: View,
action: Int,
args: Bundle?
): Boolean
Performs the specified accessibility action on the view. For possible accessibility actions look at AccessibilityNodeInfo
.
The default implementation behaves as View#performAccessibilityAction(int, Bundle)
for the case of no accessibility delegate been set.
Parameters | |
---|---|
action |
Int: The action to perform. |
host |
View: This value cannot be null . |
args |
Bundle?: This value may be null . |
Return | |
---|---|
Boolean |
Whether the action was performed. |
sendAccessibilityEvent
open fun sendAccessibilityEvent(
host: View,
eventType: Int
): Unit
Sends an accessibility event of the given type. If accessibility is not enabled this method has no effect.
The default implementation behaves as View#sendAccessibilityEvent(int)
for the case of no accessibility delegate been set.
Parameters | |
---|---|
host |
View: The View hosting the delegate. This value cannot be null . |
eventType |
Int: The type of the event to send. |
sendAccessibilityEventUnchecked
open fun sendAccessibilityEventUnchecked(
host: View,
event: AccessibilityEvent
): Unit
Sends an accessibility event. This method behaves exactly as sendAccessibilityEvent(android.view.View,int)
but takes as an argument an empty AccessibilityEvent
and does not perform a check whether accessibility is enabled.
The default implementation behaves as View#sendAccessibilityEventUnchecked(AccessibilityEvent)
for the case of no accessibility delegate been set.
Parameters | |
---|---|
host |
View: The View hosting the delegate. This value cannot be null . |
event |
AccessibilityEvent: The event to send. This value cannot be null . |