get Text from ViewGroup - Android User Interface

Android examples for User Interface:ViewGroup

Description

get Text from ViewGroup

Demo Code


//package com.java2s;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Main {
    public static String getText(ViewGroup contentView, int[] path) {
        View view = getView(contentView, path);
        if (!(view instanceof TextView)) {
            return "";
        }/*  ww  w. java 2s .co m*/

        return ((TextView) view).getText().toString();
    }

    public static View getView(ViewGroup root, int[] path) {
        ViewGroup current = root;
        for (int i = 0; i < path.length; i++) {
            if (current.getChildCount() <= path[i]) {
                return null;
            }
            View child = current.getChildAt(path[i]);
            if (i == path.length - 1) {
                return child;
            }
            if (!(child instanceof ViewGroup)) {
                return null;
            }
            current = (ViewGroup) child;
        }
        return null;
    }
}

Related Tutorials