Reputation: 23
When Accessibility Talkback is ON. I have a requirement to show my view with some custom actions when the user draws angular gesture i.e. swipe up and right. Similar like Gmail messages as shown in the pic below enter image description here
this popup is shown when the user draws action gesture i.e. swipe up and right when the focus is on any message. Action menu
Upvotes: 2
Views: 2769
Reputation: 81
In order to add an accessibility action the below line of code suffices, using ViewCompat which is backwards compatible to API 21.
ViewCompat.addAccessibilityAction(viewToAddActionTo,"String to describe what the action does", (v,b) -> actionCalled);
//a sample request would look like
ViewCompat.addAccessibilityAction(swipeLayout,"Delete Item in List",(v,b) -> deleteItem(position);
Hopefully this suffices, the action to be done i.e delete, star, archive (Similar to Gmail) can be added to custom views using this. Key Points are to figure out which View needs the action and what should the action perform.
Upvotes: 1
Reputation: 597
You can add a custom action to the AccessibilityNodeInfo
object inside the onInitializeAccessibilityNodeInfo
method of an AccessibilityDelegate
.
If the action is then selected by the user, the performAccessibilityAction
method is called on the View.
In the example "myCustomAction" is the text that is displayed to the user.
MyCustomView.kt
init {
accessibilityDelegate = object : View.AccessibilityDelegate() {
override fun onInitializeAccessibilityNodeInfo(host: View?, info: AccessibilityNodeInfo?) {
super.onInitializeAccessibilityNodeInfo(host, info)
info?.addAction(AccessibilityNodeInfo.AccessibilityAction(R.id.myCustomAccessibilityEvent, "myCustomAction"));
}
}
}
override fun performAccessibilityAction(action: Int, arguments: Bundle?): Boolean {
if (action == R.id.myCustomAccessibilityEvent) Log.d("TAG", "Accessibility event triggered")
return true;
}
res/values/accessibility.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="myCustomAccessibilityEvent"/>
</resources>
Upvotes: 1