karlkar
karlkar

Reputation: 227

Android - how to clear accessibility focus

I would like to clear accessibility focus. But in my case it is not easy as object that currently has focus is not accessible in the scope, so I cannot call on it clearAccessibilityFocus(). In this moment I request focus on other object, that I have access to and then clear focus, but it is not good solution. Can you help me with that?

Upvotes: 1

Views: 11836

Answers (4)

Pavel Kolesnikov
Pavel Kolesnikov

Reputation: 41

There are two possible situations. If you need to remove focus from real view then you can act like this:

view.performAccessibilityAction(
    AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS, 
    null
)

But if you need to remove focus from virtual view then you should do something like this:

view.accessibilityNodeProvider?.performAction(
    virtualViewId,
    AccessibilityNodeInfoCompat.ACTION_CLEAR_ACCESSIBILITY_FOCUS,
    null
)

Upvotes: 0

Sidhu
Sidhu

Reputation: 21

I guess you can go for:

myView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED);

Upvotes: 2

akiyame
akiyame

Reputation: 51

You can use

view.performAccessibilityAction(
            AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS, null);

to clear the accessibility focus.

Upvotes: 4

Ali
Ali

Reputation: 2487

You can call this on view.

setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);

I was having a problem where accessibility was leaked on the fragment which was added on top of other fragment. so onCreateView() of the second fragment

container.getFocusedChild().setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);

Upvotes: 1

Related Questions