How to change Android talkback instructions for double tap and long press
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);
}
});
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);
}
});
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.