Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fire a custom accessibility event from an ActionBar button click

I am trying to fire a custom AccessibilityEvent using the AccessibilityManager and TalkBack.

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

When I try to run this, I do not get the expected TalkBack message. I am pretty sure that I'm missing something basic with instantiating an AccessibilityEvent.

I am also not sure whether I need to use, or how to apply AccessibilityDelegates here because the callback is coming from a MenuItem rather than a View. I know I can call findViewById to get the view for this MenuItem, but I am not very knowledgeable on these APIs.

Any guidance on these two points would be great!

The problem in question is described basically by the following pseudocode:

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();
        }
    }
}}
like image 628
lewiSnort Avatar asked Mar 11 '14 22:03

lewiSnort


2 Answers

Try to fire accessibility events using 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);
}
like image 96
GokulaKrishnanM Avatar answered Sep 27 '22 18:09

GokulaKrishnanM


Although it is an old question, I'm going to publish my answer because the answer gave before 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);
            }
        }
    }
}
like image 38
jlggary Avatar answered Sep 27 '22 18:09

jlggary