Added in API level 16

AccessibilityNodeProvider


abstract class AccessibilityNodeProvider
kotlin.Any
   ↳ android.view.accessibility.AccessibilityNodeProvider

This class is the contract a client should implement to enable support of a virtual view hierarchy rooted at a given view for accessibility purposes. A virtual view hierarchy is a tree of imaginary Views that is reported as a part of the view hierarchy when an AccessibilityService explores the window content. Since the virtual View tree does not exist this class is responsible for managing the AccessibilityNodeInfos describing that tree to accessibility services.

The main use case of these APIs is to enable a custom view that draws complex content, for example a monthly calendar grid, to be presented as a tree of logical nodes, for example month days each containing events, thus conveying its logical structure.

A typical use case is to override View#getAccessibilityNodeProvider() of the View that is a root of a virtual View hierarchy to return an instance of this class. In such a case this instance is responsible for managing AccessibilityNodeInfos describing the virtual sub-tree rooted at the View including the one representing the View itself. Similarly the returned instance is responsible for performing accessibility actions on any virtual view or the root view itself. For example:

Note: Consider using a androidx.customview.widget.ExploreByTouchHelper, a utility extension of AccessibilityNodeProvider, to simplify many aspects of providing information to accessibility services and managing accessibility focus.

Kotlin

// "view" is the View instance on which this class performs accessibility functions.
  class MyCalendarViewAccessibilityDelegate(
        private var view: MyCalendarView) : AccessibilityDelegate() {
      override fun getAccessibilityNodeProvider(host: View): AccessibilityNodeProvider {
          return object : AccessibilityNodeProvider() {
              override fun createAccessibilityNodeInfo(virtualViewId: Int):
                      AccessibilityNodeInfo? {
                  when (virtualViewId) {
                      <var>host-view-id</var>-> {
                          val node = AccessibilityNodeInfo.obtain(view)
                          node.addChild(view, <var>child-view-id</var>)
                          // Set other attributes like screenReaderFocusable
                          // and contentDescription.
                          return node
                      }
                      <var>child-view-id</var>-> {
                          val node = AccessibilityNodeInfo
                                  .obtain(view, virtualViewId)
                          node.setParent(view)
                          node.addAction(ACTION_SCROLL_UP)
                          node.addAction(ACTION_SCROLL_DOWN)
                          // Set other attributes like focusable and visibleToUser.
                          node.setBoundsInScreen(
                                  Rect(<var>coords-of-edges-relative-to-screen</var>))
                          return node
                      }
                      else -> return null
                  }
              }
 
              override fun performAction(
                  virtualViewId: Int,
                  action: Int,
                  arguments: Bundle
              ): Boolean {
                  if (virtualViewId == <var>host-view-id</var>) {
                      return view.performAccessibilityAction(action, arguments)
                  }
                  when (action) {
                      ACTION_SCROLL_UP.id -> {
                          // Implement logic in a separate method.
                          navigateToPreviousMonth()
 
                          return true
                      }
                      ACTION_SCROLL_DOWN.id ->
                          // Implement logic in a separate method.
                          navigateToNextMonth()
 
                          return true
                      else -> return false
                  }
              }
          }
      }
  }
  

Java

final class MyCalendarViewAccessibilityDelegate extends AccessibilityDelegate {
      // The View instance on which this class performs accessibility functions.
      private final MyCalendarView view;
 
      MyCalendarViewAccessibilityDelegate(MyCalendarView view) {
          this.view = view;
      }
 
      @Override
      public AccessibilityNodeProvider getAccessibilityNodeProvider(View host) {
          return new AccessibilityNodeProvider() {
              @Override
              @Nullable
              public AccessibilityNodeInfo createAccessibilityNodeInfo(int virtualViewId) {
                  if (virtualViewId == <var>host-view-id</var>) {
                      AccessibilityNodeInfo node = AccessibilityNodeInfo.obtain(view);
                      node.addChild(view, <var>child-view-id</var>);
                      // Set other attributes like screenReaderFocusable and contentDescription.
                      return node;
                  } else if (virtualViewId == <var>child-view-id</var>) {
                      AccessibilityNodeInfo node =
                          AccessibilityNodeInfo.obtain(view, virtualViewId);
                      node.setParent(view);
                      node.addAction(ACTION_SCROLL_UP);
                      node.addAction(ACTION_SCROLL_DOWN);
                      // Set other attributes like focusable and visibleToUser.
                      node.setBoundsInScreen(
                          new Rect(<var>coordinates-of-edges-relative-to-screen</var>));
                      return node;
                  } else {
                      return null;
                  }
              }
 
              @Override
              public boolean performAction(int virtualViewId, int action, Bundle arguments) {
                  if (virtualViewId == <var>host-view-id</var>) {
                      return view.performAccessibilityAction(action, arguments);
                  }
 
                  if (action == ACTION_SCROLL_UP.getId()) {
                      // Implement logic in a separate method.
                      navigateToPreviousMonth();
 
                      return true;
                  } else if (action == ACTION_SCROLL_DOWN.getId()) {
                      // Implement logic in a separate method.
                      navigateToNextMonth();
 
                      return true;
                  } else {
                      return false;
                  }
              }
          };
      }
  }
  

Summary

Constants
static Int

The virtual id for the hosting View.

Public constructors

Public methods
open Unit
addExtraDataToAccessibilityNodeInfo(virtualViewId: Int, info: AccessibilityNodeInfo!, extraDataKey: String!, arguments: Bundle!)

Adds extra data to an AccessibilityNodeInfo based on an explicit request for the additional data.

open AccessibilityNodeInfo?

Returns an AccessibilityNodeInfo representing a virtual view, such as a descendant of the host View, with the given virtualViewId or the host View itself if virtualViewId equals to HOST_VIEW_ID.

open MutableList<AccessibilityNodeInfo!>?
findAccessibilityNodeInfosByText(text: String!, virtualViewId: Int)

Finds AccessibilityNodeInfos by text.

open AccessibilityNodeInfo?
findFocus(focus: Int)

Find the virtual view, such as a descendant of the host View, that has the specified focus type.

open Boolean
performAction(virtualViewId: Int, action: Int, arguments: Bundle?)

Performs an accessibility action on a virtual view, such as a descendant of the host View, with the given virtualViewId or the host View itself if virtualViewId equals to HOST_VIEW_ID.

Constants

HOST_VIEW_ID

Added in API level 21
static val HOST_VIEW_ID: Int

The virtual id for the hosting View.

Value: -1

Public constructors

AccessibilityNodeProvider

AccessibilityNodeProvider()

Public methods

addExtraDataToAccessibilityNodeInfo

Added in API level 26
open fun addExtraDataToAccessibilityNodeInfo(
    virtualViewId: Int,
    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 a virtual view offers to provide additional data.

Parameters
virtualViewId Int: The virtual view id used to create the node
info AccessibilityNodeInfo!: The info to which to add the extra data
extraDataKey String!: A key specifying the type of extra data to add to the info. The extra data should be added to the Bundle returned by the info's AccessibilityNodeInfo#getExtras method.
arguments Bundle!: A Bundle holding any arguments relevant for this request.

createAccessibilityNodeInfo

Added in API level 16
open fun createAccessibilityNodeInfo(virtualViewId: Int): AccessibilityNodeInfo?

Returns an AccessibilityNodeInfo representing a virtual view, such as a descendant of the host View, with the given virtualViewId or the host View itself if virtualViewId equals to HOST_VIEW_ID.

A virtual descendant is an imaginary View that is reported as a part of the view hierarchy for accessibility purposes. This enables custom views that draw complex content to report them selves as a tree of virtual views, thus conveying their logical structure.

The implementer is responsible for obtaining an accessibility node info from the pool of reusable instances and setting the desired properties of the node info before returning it.

Parameters
virtualViewId Int: A client defined virtual view id.
Return
AccessibilityNodeInfo? A populated AccessibilityNodeInfo for a virtual descendant or the host View. This value may be null.

findAccessibilityNodeInfosByText

Added in API level 16
open fun findAccessibilityNodeInfosByText(
    text: String!,
    virtualViewId: Int
): MutableList<AccessibilityNodeInfo!>?

Finds AccessibilityNodeInfos by text. The match is case insensitive containment. The search is relative to the virtual view, i.e. a descendant of the host View, with the given virtualViewId or the host View itself virtualViewId equals to HOST_VIEW_ID.

Parameters
virtualViewId Int: A client defined virtual view id which defined the root of the tree in which to perform the search.
text String!: The searched text.
Return
MutableList<AccessibilityNodeInfo!>? A list of node info. This value may be null.

findFocus

Added in API level 19
open fun findFocus(focus: Int): AccessibilityNodeInfo?

Find the virtual view, such as a descendant of the host View, that has the specified focus type.

Parameters
focus Int: The focus to find. One of AccessibilityNodeInfo#FOCUS_INPUT or AccessibilityNodeInfo#FOCUS_ACCESSIBILITY.
Return
AccessibilityNodeInfo? The node info of the focused view or null.

performAction

Added in API level 16
open fun performAction(
    virtualViewId: Int,
    action: Int,
    arguments: Bundle?
): Boolean

Performs an accessibility action on a virtual view, such as a descendant of the host View, with the given virtualViewId or the host View itself if virtualViewId equals to HOST_VIEW_ID.

Parameters
virtualViewId Int: A client defined virtual view id.
action Int: The action to perform.
arguments Bundle?: Optional action arguments. This value may be null.
Return
Boolean True if the action was performed.