I'm creating a custom view (extending View class) and playing with Accessibility API to understand, how it works. Below is my code, where:
- I check for
MotionEvent
equal toACTION_HOVER_ENTER
and then inside it send AccessibilityEvent of typeTYPE_VIEW_HOVER_ENTER
. - I'm catching my
AccessibilityEvent
inonPopulateAccessibilityEvent
. Then I'm adding my custom text into event's text, which is added just fine.
As a result, when I hover over my view, all is working fine (confirmed by my logs), but TalkBack doesn't say my custom text. The only way I was able to make TalkBack to say custom text is to use setContentDescription("custom text...")
.
But, the way I understand the API, it should be possible to set different text depending on the type of MotionEvent
and correspondingly a type of AccessibilityType
.
My question - can someone explain to me please, how can I make TalkBack to speak my custom text, which I can set depending on type of event?
@Override
public boolean onHoverEvent(MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_HOVER_ENTER:
Log.d("test", "onHoverEvent: ACTION_HOVER_ENTER"); // <-- this is triggered correctly on hover enter
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
return true;
}
return super.onHoverEvent(event);
}
@Override
public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
super.onPopulateAccessibilityEvent(event);
if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_HOVER_ENTER) {
Log.d("test", "onPopulateAccessibilityEvent");
CharSequence text = "this is a test";
Log.d("test", "text before: " + event.getText()); // text before is empty, i.e. "[]"
event.getText().add(text);
Log.d("test", "text after: " + event.getText()); // text after is "[this is a test]", but TalkBack is silent
}
}