5

I am trying to change the text announced by TalkBack when an ImageView is focused through accessibility.

The Android documentation states that we should create an AccessibilityDelegate, and override onPopulateAccessibilityEvent (I am using the support library because I am also supporting GingerBread)

Thus, my code is the following:

public static void setImageDelegate(View view) {
    AccessibilityDelegateCompat delegate = new AccessibilityDelegateCompat() {
        @Override
        public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
            event.getText().add(event.getContentDescription() + ", image");
        }
    };
    ViewCompat.setAccessibilityDelegate(view, delegate);
}

When I call this function on my imageview, the delegate gets set, but the modified text is not being read. It simply reads the original content description. Am I doing something wrong or missing something about the accessibility functions?

Stepping through the code, it seems to be adding the correct text, but still, no change in spoken text.

Note: the above is a contrived example, content description could be used, but I'm trying to figure out why it doesn't work before I try it on custom views.

Allen Z.
  • 340
  • 1
  • 5
  • 10

1 Answers1

7

In ICS and above, TalkBack doesn't use the accessibility event text in most cases. Instead, it checks the text and content description of the AccessibilityNodeInfo exposed by the view. You would need to override onInitializeAccessibilityNodeInfo.

In most cases, though, you would just want to call View.setContentDescription.

In this particular case, you shouldn't set anything since TalkBack handles speaking control types and capabilities. We strongly advise developers against adding descriptions like "button" or "image."

alanv
  • 23,966
  • 4
  • 93
  • 80
  • Thanks for the response alanv. Many times, I have issues with overriding onInitializeAccessibilityNodeInfo. I submitted an issue: https://code.google.com/p/android/issues/detail?id=76423&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars, would you happen to know anything about this? – Allen Z. Sep 23 '14 at 21:46
  • 2
    That issue is currently assigned to me. We're looking into it! – alanv Sep 24 '14 at 04:59
  • Hi! Is there a way to add prefix or suffix to talkBack for a particular view? I have been struggling with it for quite a sometime. – Abhi Nov 02 '22 at 06:45