javaandroidaccessibilityaccessibilityservice

How to use an android accessibility service to detect which view the user touched?


There's something that android TalkBack does that I want to do too. Specifically it's to identify the view that the user touched. I made this so far:

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED) {

        //This only shows the package of the activity that the view is in, I want to identify the view itself
        Toast.makeText(this, "" + event.getSource().getPackageName(), Toast.LENGTH_LONG).show();
    }
}

Solution

  • You can get the view's id, but I think you can only access the AccessibilityNodeInfo associated with the view - I don't think you have access to the view itself. You can check the docs for more information on this.

        // kotlin code
        event.source.viewIdResourceName
        // or
        findFocusedViewInfo().viewIdResourceName
    

    According to the docs:

    Gets the fully qualified resource name of the source view's id.

    Note: The primary usage of this API is for UI test automation and in order to report the source view id of an AccessibilityNodeInfo the client has to set the AccessibilityServiceInfo#FLAG_REPORT_VIEW_IDS flag when configuring the AccessibilityService.