Android fires custom accessibility event from pressing ActionBar button

I am trying to start custom AccessibilityEventusing AccessibilityManager and TalkBack.

A use case for an event is when the user clicks on the action bar, the fragment polls the list of objects, and then changes its AccessibilityEvent content based on the size of the list.

When I try to run this, I do not receive the expected TalkBack message. I'm sure I missed something basic by creating an instance of AccessibilityEvent.

I'm also not sure whether I need to use or how to apply AccessibilityDelegatehere, because the callback comes from MenuItem, not from View. I know that I can call findViewByIdto get an idea for this MenuItem, but I'm not very good at these APIs.

Any guidance on these two points would be great!

The problem in question is mainly described by the following pseudo-code:

public class MyFragment extends Fragment {

//...

private List<Pojo> mPojoList;

//...

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.the_id_for_my_menuitem) {
        if (booleanCheck() && !mPojoList.isEmpty()) {

            //create the Accessibility event
            final AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_CLICKED);
            event.setContentDescription(String.format("deleting %2d pojos", mPojoList.size()));

            //Send a custom accessibility event to let the user know that we're deleting X objects.
            final AccessibilityManager mgr = (AccessibilityManager) this.getActivity().getSystemService(Context.ACCESSIBILITY_SERVICE);

            //PROBLEM: We're not seeing this event come through in TalkBack.
            mgr.sendAccessibilityEvent(event);

            //Delete the objects.
            myDeleteObjectsFunction();
        }
    }
}}
+4
source share
2 answers

Try activating accessibility events using the View object.

AccessibilityEvent event = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_CLICKED);
event.setContentDescription(String.format("deleting %2d pojos", mPojoList.size()));

View view = getActivity().findViewById(R.id.child_view);

ViewParent parent = view.getParent();
if (parent != null) {
  parent.requestSendAccessibilityEvent(view, event);
}
+2
source

Although this is an old question, I am going to post my answer because the answer I gave earlier did not work for me.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.the_id_for_my_menuitem) {
        if (booleanCheck() && !mPojoList.isEmpty()) {

            AccessibilityManager manager = (AccessibilityManager)this.getSystemService(Context.ACCESSIBILITY_SERVICE);
            if(manager.isEnabled()){
                AccessibilityEvent event = AccessibilityEvent.obtain();
                event.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
                event.setClassName(getClass().getName());
                event.getText().add(*yourString*);

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    event.setSource(findViewById(*yourButton*));
                }
                manager.sendAccessibilityEvent(event);
            }
        }
    }
}
+1
source

Source: https://habr.com/ru/post/1531254/


All Articles