Search code examples
androidandroid-viewpageraccessibilitytalkback

Talkback does not announce Page information when ViewPager is launched


I have a simple ViewPager. A very basic one. When we enable accessibility, when user lands on the ViewPager, the talkback announces the Fragment contents inside the ViewPager, ending with "inside Pager" phrase. We we swipe to page 2, it ends with "Page 2 of 3" phrase. When we move back to page 1, it now announces "Page 1 of 3". I want to announce the talkback "Page 1 of 3" even when user lands on the ViewPager for first time. I see this is default android behaviour, but why it is so? Is there any way to achieve this?


Solution

  • this kind of thing is super frustrating for developers, but not screen reader users.

    I see this is default android behaviour, but why it is so?

    The key idea is not to be too verbose. A screen reader user is aware that when a ViewPager is announced for the first time, that they're on the first page. Because they might be searching for something else, Google appears to have made the decision to rather wait for the user to explore before giving more information.

    The source code for this can be found within TalkBack:

    private @Nullable CharSequence getDescriptionForPageEvent(
        AccessibilityEvent event, AccessibilityNodeInfo source) {
        final int fromIndex = (event.getFromIndex() + 1);
        final int itemCount = event.getItemCount();
        if ((fromIndex <= 0) || (itemCount <= 0)) {
            return null;
        }
        ...
    

    The fromIndex variable indicates where the user is coming from, and if it's 0, or "never been anywhere else before," it just returns null.

    This may not be the same for other screen readers, such as the commentary screen reader, and it may be subject to change in Samsung's version (yes it's different), and it might be completely different in VoiceView (Amazon's device screen reader). So don't try hack it in!

    Please note, I'm not affliated with Google or TalkBack in any official capacity, I just decided to go learn a bit about the TalkBack source to make a developer friendly version.