Gets the first child it finds in a parent matched from an instance (recursive) - Android User Interface

Android examples for User Interface:ViewGroup

Description

Gets the first child it finds in a parent matched from an instance (recursive)

Demo Code


//package com.java2s;

import android.view.View;
import android.view.ViewGroup;

public class Main {
    /**// www .  j a  va  2s .c  o m
     * Gets the first child it finds in a parent matched from an instance (recursive)
     * @param parent The parent view
     * @param instance The instance to check
     * @return The found view, or null
     */
    public static View getFirstChildByInstance(ViewGroup parent,
            Class instance) {
        View retView = null;
        int childCount = parent.getChildCount();

        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            View child = parent.getChildAt(childIndex);

            if (child.getClass() == instance) {
                return child;
            }

            if (child instanceof ViewGroup) {
                View v = getFirstChildByInstance((ViewGroup) child,
                        instance);

                if (v != null) {
                    return v;
                }
            }
        }

        return retView;
    }
}

Related Tutorials