find Nodes By Content Desc - Android android.view.accessibility

Android examples for android.view.accessibility:AccessibilityNodeInfo

Description

find Nodes By Content Desc

Demo Code


//package com.java2s;

import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

public class Main {
    private static final String TAG = "ASUtil";

    public static AccessibilityNodeInfo findNodesByContentDesc(
            AccessibilityNodeInfo root, String targetContentDesc) {
        if (root == null || TextUtils.isEmpty(targetContentDesc)) {
            return null;
        }//from   ww  w.  j  a  v a2s  .c  om

        CharSequence contentDesc = root.getContentDescription();


        if (!TextUtils.isEmpty(contentDesc)
                && contentDesc.equals(targetContentDesc)) {
            return root;
        } else {
            for (int i = 0; i < root.getChildCount(); i++) {
                AccessibilityNodeInfo childNode = root.getChild(i);
                AccessibilityNodeInfo result = findNodesByContentDesc(
                        childNode, targetContentDesc);
                if (result != null) {
                    return result;
                }
            }
            return null;
        }
    }
}

Related Tutorials