Search code examples
androidaccessibilityservice

android Accessibility focusSearch does not return any node


My Accessibility service would go through the focusable nodes. I am trying to use the focusSearch function for this, but that is not giving any node back. But the layout contains focusable items. Here is the layout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:text="Button" />

    <TextView
        android:id="@+id/text"
        android:layout_width="122dp"
        android:layout_height="fill_parent"
        android:text="Hello World2" />

        <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:focusable="true"
        android:text="Button2" />


</LinearLayout>

So what I tried is to find the first button:

AccessibilityNodeInfo root = getRootInActiveWindow();
AccessibilityNodeInfo node1 = null;
node1 = root.findAccessibilityNodeInfosByViewId("my.app:id/button1").get(0);
//returns button view
node1 = root.focusSearch(View.FOCUS_DOWN); //returns null
node1 = root.focusSearch(View.FOCUS_LEFT); //returns null
node1 = root.focusSearch(View.FOCUS_RIGHT); //returns null
node1 = root.focusSearch(View.FOCUS_FORWARD); //returns null

However using findFocus it gives back the framelayout

node1 = root.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
//returns node of the main framelayout

Then I used this node for next search, but nothing found again.

node1 = node1.focusSearch(View.FOCUS_FORWARD); //returns null

And when I call findfocus instead of focusSearch i got the same node back

node1 = node1.findFocus(AccessibilityNodeInfo.FOCUS_ACCESSIBILITY);
//returns the same node

So the question is how can I go through on the focusable nodes within a layout?


Solution

  • This is a common misconception. What you are searching for is objects that can take input focus (think EditText controls). This is fundamentally different from Accessibility Focus. There are no input focusable controls in your layout.

    The functions you want are:

    someAccessibilityNodeInfo.getTraversalAfter();
    

    and

    someAccessibilityNodeInfo.getTraversalBefore();
    

    There is no up/down equivalent. Though you could calculate this however you want. You could store the entire heirarchy of accessibility focusable nodes in an array, and calculate up/down based on x and y positions.