How to change the prompt for using TalkBack for interactive viewing on Android?

By default, for viewing on Android, usage tips will be displayed that are read aloud (if TalkBack is turned on and the user focuses on this view) after the content description:

"Double tap to activate"

Can I change this so that it reads something less abstract and more specific to my application? How:

"Double tap to play the video"

+5
source share
1 answer

Yes, it is absolutely possible!

Overriding the onInitializeAccessibilityNodeInfo

If you have a custom view, you can override the onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) method and add an action with the ACTION_CLICK identifier to override the label:

 @Override public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { super.onInitializeAccessibilityNodeInfo(info); info.addAction( new AccessibilityNodeInfo.AccessibilityAction( AccessibilityNodeInfo.ACTION_CLICK, "play video" ) ); } 

If this view has a click listener, then by adding this new Action you will override the default label, so TalkBack will say โ€œDouble-clickโ€ instead.

This is only available in API 21 โ€” what if you want something that worked in a lower version of the API, or want to set a custom usage hint on a custom view? You can use ViewCompat and AccessibilityDelegateCompat !

Use AccessibilityDelegate instead

This is very similar - you can override the equivalent method in a custom AccessibilityDelegate, which you extend:

 public static class PlayVideoAccessibilityDelegate extends AccessibilityDelegateCompat { @Override public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) { super.onInitializeAccessibilityNodeInfo(host, info); info.addAction( new AccessibilityNodeInfoCompat.AccessibilityActionCompat( AccessibilityNodeInfoCompat.ACTION_CLICK, "play video" ) ); } } 

then to use it, you set the delegate using ViewCompat :

 ViewCompat.setAccessibilityDelegate(playButton, new PlayVideoAccessibilityDelegate()); 

Using accessibilitools

Novoda has a utility library that will help with access to Android. This includes some tools to help establish usage tips:

 UsageHintsAccessibilityDelegate delegate = new UsageHintsAccessibilityDelegate(resources); delegate.setClickLabel("play video"); ViewCompat.setAccesibilityDelegate(playButton, delegate); 

I wrote a block package which is a review of accessibilitools (I am also the author of the library).

+3
source

Source: https://habr.com/ru/post/1262390/


All Articles