Android: change accessibility order programmatically

Viewed 2729

Is there a way to change the order that the screen reader, in android case the TalkBack, has to read the accessible elements? Like if I have 3 elements, initially read in order b1, b2, b3, after I press a certain button the order change and become b3, b1, b2?

EDIT This is what I tried following @Nikos Hidalgo hint

CODE:

    Button b1,b2,b3, changeorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout linearlayout = findViewById(R.id.linearlayout);

    changeorder = new Button(this);
    changeorder.setText("Presso to change order");
    changeorder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("BUTTON","Clicked change order button");

            changeorder.setAccessibilityTraversalBefore(b3.getId());
            b3.setAccessibilityTraversalAfter(changeorder.getId());
            b3.setAccessibilityTraversalBefore(b1.getId());
            b1.setAccessibilityTraversalAfter(b3.getId());
            b1.setAccessibilityTraversalBefore(b2.getId());
            b2.setAccessibilityTraversalAfter(b1.getId());
            changeorder.setText("Order changed b3,b1,b2");

        }
    });
    changeorder.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
    linearlayout.addView(changeorder);

    b1 = new Button(this);
    b1.setText("BUTTON 1");
    b1.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
    linearlayout.addView(b1);


    b2 = new Button(this);
    b2.setText("BUTTON 2");
    b2.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
    linearlayout.addView(b2);


    b3 = new Button(this);
    b3.setText("BUTTON 3");
    b3.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
    linearlayout.addView(b3);


    }
1 Answers

You can programmatically define/change the traversal order (order with which the views are parsed in TalkBalk) using the following methods:

public void setAccessibilityTraversalAfter (int afterId)

Sets the id of a view after which this one is visited in accessibility traversal. A screen-reader must visit the content of the other view before the content of this one. For example, if view B is set to be after view A, then a screen-reader will traverse the entire content of A before traversing the entire content of B, regardles [sic] of what traversal strategy it is using.

(source)

public void setAccessibilityTraversalBefore (int beforeId)

Sets the id of a view before which this one is visited in accessibility traversal. A screen-reader must visit the content of this view before the content of the one it precedes. For example, if view B is set to be before view A, then a screen-reader will traverse the entire content of B before traversing the entire content of A, regardles [sic] of what traversal strategy it is using.

(source)

As they’re both public methods of View you could use them like this:

findView(R.id.view1).setAccessibilityTraversalBefore(R.id.view2);

EDIT: make sure that all your views are focusable and if you're programmatically adding more views, make sure that you set the importantForAccessibility attribute.

e.g. myView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);

Related