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?

1
  • 1
    I don't think it is- I think those are the default descriptions from the talkback app itself. Commented Sep 12, 2016 at 17:49

5 Answers 5

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);
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

WOW! I was looking for something else related to TalkBack but stumbled upon this. This is nice!
How to prevent it from saying Double tap to
@IgorGanapolsky I wrote a solution to prevent it from saying Double tap to here. stackoverflow.com/a/64475769/6482087
How to do the same in RemoteViews used in widgets
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.

10 Comments

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.
@fobbymaster use ViewCompat.setAccessibilityDelegate(View v, AccessibilityDelegateCompat delegate)
@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'?
@user3030130 No, it will replace the existing description of the action.
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?
|
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);
            }
        });

4 Comments

It still plays "Double tap to activate"
@IgorGanapolsky Did you ever figure out how to prevent the accessibility from saying "Double tap to activate" ?
@LoveMeSomeFood I wrote a solution to prevent it from saying Double tap to here. stackoverflow.com/a/64475769/6482087
This disable TalkBack speaking the selection of an item
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.

Comments

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();
                }
            }

2 Comments

Why ACTION_FOCUS and not ACTION_CLICK?
This solution should work. stackoverflow.com/a/64475769/6482087

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.