Kaleb Kaleb - 4 months ago 86
Android Question

Unable to set accessibility focus on title textview

I have an EditText on an activity that requests focus when the activity launches. This is great for users who do not have TalkBack enabled. However, for accessibility (i.e. for users who have TalkBack enabled), I need to set the focus on the titlebar of the screen, instead of the EditText.

I have tried several things but have been unsuccessful at setting the focus on the titlebar's textview (I have checked that titleTextView does not return null):

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TextView titleTextView = null;
//get the tool bar's title TextView
try
{
Class<?> toolbarClass = Toolbar.class;
Field titleTextViewField = toolbarClass.getDeclaredField("mTitleTextView");
titleTextViewField.setAccessible(true);
titleTextView = (TextView)titleTextViewField.get(toolbar);
}
catch (NoSuchFieldException | IllegalAccessException e)
{
e.printStackTrace();
}

if (titleTextView != null)
{
titleTextView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
AccessibilityManager manager = (AccessibilityManager)getSystemService(Context.ACCESSIBILITY_SERVICE);
if (manager.isEnabled())
{
AccessibilityEvent e = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_FOCUSED);
e.setSource(titleTextView);
e.setEventType(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
e.setClassName(getClass().getName());
e.setPackageName(getPackageName());
e.getText().add("Message");
manager.sendAccessibilityEvent(e);
}


The titleTextView never receives focus when TalkBack is on.

Any help would be appreciated.

Answer

I was able to fix this by moving the code into the if block to only execute the code in the block if the user has TalkBack enabled. Namely, accessibilityManager.isEnabled() will only evaluate to true if TalkBack is on:

AccessibilityManager am = (AccessibilityManager)getSystemService(Context.ACCESSIBILITY_SERVICE);
if (am.isEnabled())
{
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(actionBar);
    toolbar.setFocusable(true);
    toolbar.setFocusableInTouchMode(true);
    try
    {               
        Class<?> toolbarClass = Toolbar.class;
        Field titleTextViewField = toolbarClass.getDeclaredField("mTitleTextView");
        titleTextViewField.setAccessible(true);
        TextView titleTextView = (TextView)titleTextViewField.get(toolbar);
        titleTextView.setFocusable(true);
        titleTextView.setFocusableInTouchMode(true);
    }
    catch (NoSuchFieldException | IllegalAccessException e)
    {               
        e.printStackTrace();            
    }
    toolbar.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
    AccessibilityEvent e = AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
    e.setSource(toolbar);
    e.setEventType(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED);
    e.setClassName(getClass().getName());
    e.setPackageName(getPackageName());
    am.sendAccessibilityEvent(e);
}
Comments