How to change Android talkback instructions for double tap and long press
Asked Answered
S

5

17

I have a view that has a long press action handler. I use the content description to set the message Talkback speaks when the view gets focus.

Currently it says my content description right after getting a focus, and after a short pause says:

Double tap to activate, double tap and hold for long press

I want to change this message into something like

Double tap to "action 1", double tap and hold for "action 2"

Is there a way to do so?

I looked into onPopulateAccessibilityEvent(), it gets TYPE_VIEW_ACCESSIBILITY_FOCUSED event, but I wasn't able to change the desired message.

Am I missing something simple?

Surmount answered 12/9, 2016 at 17:47 Comment(1)
I don't think it is- I think those are the default descriptions from the talkback app itself.Dripdry
G
23

It seems AccessibilityAction has changed slightly since alanv posted his answer. Here is a working solution using ViewCompat:

ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {
    @Override
    public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
        super.onInitializeAccessibilityNodeInfo(host, info);
        // A custom action description. For example, you could use "pause"
        // to have TalkBack speak "double-tap to pause."
        CharSequence description = host.getResources().getText(R.string.my_click_desc);
        AccessibilityActionCompat customClick = new AccessibilityActionCompat(
                    AccessibilityNodeInfoCompat.ACTION_CLICK, description);
        info.addAction(customClick);
    }
});
Glottis answered 21/9, 2017 at 23:11 Comment(4)
WOW! I was looking for something else related to TalkBack but stumbled upon this. This is nice!Anora
How to prevent it from saying Double tap toDomestic
@Domestic I wrote a solution to prevent it from saying Double tap to here. https://mcmap.net/q/743280/-how-do-it-disable-message-quot-double-on-tap-quot-in-view-using-talkback-accessibility-androidArchiplasm
How to do the same in RemoteViews used in widgetsMinacious
I
12

In API 21+, you can customize the action names by setting up custom actions on your View's AccessibilityNodeInfo. There are two approaches to this: 1) set an AccessibilityDelegate and override the onInitializeAccessibilityNodeInfo delegate method or 2) extend the view's class and override onInitializeAccessibilityNodeInfo.

Either way, you will be constructing a new AccessibilityAction and setting it on the node using AccessibilityNodeInfo.addAction.

If you chose to use a delegate, you would set a custom description for the click action as follows:

view.setAccessibilityDelegate(new AccessibilityDelegate() {
  @Override
  public void onInitializeAccessibilityNodeInfo(
      View v, AccessibilityNodeInfo info) {
    super.onInitializeAccessibilityNodeInfo(v, info);

    // A custom action description. For example, you could use "pause"
    // to have TalkBack speak "double-tap to pause."
    CharSequence description = getResources().getText(R.string.my_click_desc);
    AccessibilityAction customClick = new AccessibilityAction(
            AccessibilityAction.ACTION_CLICK, description);
    info.addAction(customClick);
  }
});

If you application targets API < 21, substitute the appropriate *Compat support library methods. The feature is not backported, so you won't get custom descriptions on API < 21, but you will be able to avoid explicit version checks in your application code.

Innis answered 14/9, 2016 at 0:53 Comment(10)
Applying the *Compat to this - could you post an example? setAccessibilityDelegate doesn't take a compat nor is there a setter for the compat version.Azine
@Azine use ViewCompat.setAccessibilityDelegate(View v, AccessibilityDelegateCompat delegate)Paramilitary
@alanv, this will only add to the existing list right? It will still speak 'double tap to activate' followed by your 'double tap to pause'?Figment
@Figment No, it will replace the existing description of the action.Innis
Right, but if there are other actions for the view, especially the default ones that you don't add, this action is being spoken along with the other actions. Do you know how to remove all actions and just add yours?Figment
Post a new question. This is outside the scope of the original question.Innis
This is only replacing the last word for me. the first part of the sentence "double tap to" is still available. Is it possible to replace the whole sentence?Syconium
Replacing the whole sentence wouldn't make sense because you can't know what the accessibility service's user interface is like.Intermingle
@RedM Where you able to figure out how to prevent talkback from saying "Double tap to ___"Baroness
How to do the same for RemoteView used to make widget?Minacious
G
8

Use the code below for those who want to remove the all phrase ie. "double tap to activate", "double tap and hold for long press".

mSubTitleText = (TextView) view.findViewById(R.id.txt_subtitle);

 ViewCompat.setAccessibilityDelegate(mSubTitleText, new AccessibilityDelegateCompat() {
            @Override
            public void onPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
                host.setClickable(false);
                host.setLongClickable(false);
            }
        });
Georgettegeorgi answered 4/2, 2019 at 7:27 Comment(4)
It still plays "Double tap to activate"Domestic
@Domestic Did you ever figure out how to prevent the accessibility from saying "Double tap to activate" ?Baroness
@Baroness I wrote a solution to prevent it from saying Double tap to here. https://mcmap.net/q/743280/-how-do-it-disable-message-quot-double-on-tap-quot-in-view-using-talkback-accessibility-androidArchiplasm
This disable TalkBack speaking the selection of an itemWolfish
W
1

Here is one sample:

ViewCompat.setAccessibilityDelegate(set_actions_button, object : AccessibilityDelegateCompat() {
    override fun onInitializeAccessibilityNodeInfo(v: View, info: AccessibilityNodeInfoCompat) {
        super.onInitializeAccessibilityNodeInfo(v, info)
        info.addAction(AccessibilityActionCompat(
            AccessibilityNodeInfoCompat.ACTION_CLICK, "Edit note"))
        info.addAction(AccessibilityActionCompat(
            AccessibilityNodeInfoCompat.ACTION_LONG_CLICK, "Copy note"))
    }
})

Take a look at this article which explains very well.

Wolfish answered 6/9, 2021 at 10:45 Comment(0)
G
0

Use the code below for those who want to remove the all phrase ie. "double tap to".

ViewCompat.setAccessibilityDelegate(view, new AccessibilityDelegateCompat() {
        @Override
        public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCompat info) {
            super.onInitializeAccessibilityNodeInfo(host, info);
            info.addAction(AccessibilityNodeInfoCompat.ACTION_FOCUS);
        }
});

This is basically calling the below code and requestFocus does not have any default talkback announcement associated with it.

case AccessibilityNodeInfo.ACTION_FOCUS: {
                if (!hasFocus()) {
                    // Get out of touch mode since accessibility
                    // wants to move focus around.
                    getViewRootImpl().ensureTouchMode(false);
                    return requestFocus();
                }
            }
Grussing answered 18/12, 2017 at 20:9 Comment(2)
Why ACTION_FOCUS and not ACTION_CLICK?Domestic
This solution should work. https://mcmap.net/q/743280/-how-do-it-disable-message-quot-double-on-tap-quot-in-view-using-talkback-accessibility-androidArchiplasm

© 2022 - 2024 — McMap. All rights reserved.