get ViewGroup Title Text - Android User Interface

Android examples for User Interface:ViewGroup

Description

get ViewGroup Title Text

Demo Code


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

public class Main {
    public static String getTitleText(ViewGroup contentView) {
        return getText(contentView, new int[] { 0, 1 }); // Empirically
        // determined
    }/*from   w w w.  j  av a 2s  . co  m*/

    public static String getText(ViewGroup contentView, int[] path) {
        View view = getView(contentView, path);
        if (!(view instanceof TextView)) {
            return "";
        }

        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